简体   繁体   English

如何使用AWS S3托管静态express / nodeJS页面?

[英]How do I use AWS S3 to host a static express/nodeJS page?

I am new to nodejs/express/coding in general so my apologies if this isn't extremely clear. 我一般对Node.js / express / coding都是陌生的,所以如果这不是很清楚,我深表歉意。 I am doing a code challenge for a job. 我正在为工作做代码挑战。

I have most of the project done. 我完成了大部分项目。 Part of the challenge is to have it uploaded to a bucket on S3. 挑战的一部分是将其上传到S3的存储桶中。 I created a bucket, that is all done. 我创建了一个存储桶,就完成了。 My problem is there needs to be a index.html in the root folder of the bucket (I think). 我的问题是,存储桶的根文件夹中需要有一个index.html(我认为)。 All of my html pages (three of them) sit inside of the views directory. 我所有的html页面(其中三个)都位于views目录中。 When I try to put my index.html in the root folder on cloud9, it says index.html can't be found in the views directory (obviously, since I moved it out). 当我尝试将index.html放在cloud9的根文件夹中时,它表示在views目录中找不到index.html(显然,因为我将其移出了)。 Can I set the views directory to be in the root folder? 我可以将views目录设置在根文件夹中吗?

Is there a way with express/nodeJS to have all of the files in the root folder? express / nodeJS是否可以将所有文件都放在根文件夹中? Or is there a way to keep my views folder as it is in Cloud9 and have everything run like it does from there, except in S3? 还是有一种方法可以将我的views文件夹保留在Cloud9中,并像在S3中一样从那里运行所有内容? I must be missing something. 我肯定错过了什么。 I am completely lost as to how to host this app on S3. 我完全不知道如何在S3上托管此应用程序。 Posting on here was my last resort! 这是我的不得已的方法! Thanks for any help. 谢谢你的帮助。

Let's make order. 让我们下订单。 Amazon S3 is a cloud file storage service . Amazon S3是一种云文件存储服务 It can also be used to host static assets of a website. 它也可以用来托管网站的静态资产

From what I understand, you are building something with express, using the view directory, used in general for templates and so, I suppose, you are rendering your html pages by your express application. 据我了解,您正在使用express,使用通常用于模板的view目录构建东西,因此,我想,您是通过express应用程序呈现html页面的。 This is called server side rendering and is fully incompatible with amazon s3 that can only serve files. 这称为服务器端渲染,与只能提供文件的Amazon s3完全不兼容。

Now, how can you resolve the problem (considering that you are obliged to use s3)? 现在,您如何解决该问题(考虑到您必须使用s3)? It depends. 这取决于。

If you are using express only to render your application and to serve static assets ( so no API ), you should consider some refactoring: in such case, you are basically building a web application without APIs. 如果您使用的快递呈现您的应用程序和服务静态资产( 所以没有API),你应该考虑一些重构:在这种情况下,你基本上没有建立一个API的Web应用程序。 You don't need express. 您不需要快递。 Maybe you are searching for a client side framework like Vue.js, React or Angular. 也许您正在搜索Vue.js,React或Angular等客户端框架。 To be more general, you should render your application client side. 更笼统地说, 您应该呈现应用程序客户端。

If your express server is also acting as api server, you should divide your project. 如果您的快速服务器也充当api服务器,则应划分项目。 From one side you have your express api server, deployed somewhere. 一方面,您将Express api服务器部署在某个位置。 From the other side, you have your web app, client side rendered . 另一方面,您的Web应用程序已呈现为客户端

There is another solution: you could use a prerender like this to generate static assets from your express application. 还有另一种解决办法:你可以使用预渲染这样从您的Express应用程序生成静态资产。 But if you are new to web developement, I advise you not to evaluate this option 但是,如果您不熟悉Web开发,建议您不要评估此选项

When you move your static files to S3, you will need to setup the relative paths accordingly. 将静态文件移动到S3时,需要相应地设置相对路径。

Can I set the views directory to be in the root folder? 我可以将views目录设置在根文件夹中吗?

No. Instead organize your files in S3 where index.html is the root and files with paths such as js/ css/ images/ taken from the root folder. 不能。而是在S3中组织您的文件,其中index.html是根目录,而文件(例如js / css / images /)是从根文件夹中提取的。

Note: Its important to understand that you cannot run NodeJS in S3 and instead you will be using the internal web hosting from S3 to serve the static content. 注意:重要的是要了解您不能在S3中运行NodeJS,而是将使用S3的内部Web托管来提供静态内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM