简体   繁体   English

使用NodeJS和Serv-static服务html文件

[英]Serve html file with NodeJS and serve-static

I am developing an Express web application with Node JS on Google App Engine Flexible environment (Managed VMs). 我正在Google App Engine灵活环境(托管VM)上使用Node JS开发Express Web应用程序。 Basically I only have one static HTML page that I need to serve since everything is loaded dynamically in Angular JS. 基本上,我只需要提供一个静态HTML页面,因为所有内容都是在Angular JS中动态加载的。

While reading Express best practices I noticed that they discourage the use of res.sendFile which is exactly what I've been doing in development. 在阅读Express 最佳实践时,我注意到它们不鼓励使用res.sendFile,这正是我在开发中一直在做的事情。

Since I am switching to production I'd like to use static-serve as suggested but I couldn't find any documentation that explains how to simulate res.sendFile. 由于我要切换到生产环境,因此我想按照建议使用static-serve,但找不到任何说明如何模拟res.sendFile的文档。

Below you can find my current code. 在下面可以找到我当前的代码。

var app = express();
app.use(express.static('www'));
app.get('/', oauth2.required, function (req, res) {
// ...
res.sendFile(path.join(__dirname + '/www/dashboard.html'));
// ...
});

To serve a static file you can define a static folder content in expressJS 要提供静态文件,您可以在expressJS中定义静态文件夹内容

app.use('/public', express.static(path.join(__dirname, './public')));

which mean that every files in you public folder will be served as static content when you hit and url like mydomain.com/public/image.png 这意味着当您击中并添加URL时,如mydomain.com/public/image.png,公用文件夹中的每个文件都将用作静态内容。

EDIT: if possible in your dev environment 编辑:如果可能在您的开发环境中

You could use an nginx server to serve you static file and let you node.js server handle the dynamic content. 您可以使用nginx服务器为您提供静态文件,并让node.js服务器处理动态内容。 It is usually the most optimized solution to reduce the requests on your node.js server that is slower to server static files than nginx for example. 例如,这通常是最优化的解决方案,用于减少对node.js服务器的请求,这种请求对服务器静态文件的响应要比nginx慢。

An nginx configuration could be Nginx配置可能是

   root /home/myapp;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location /public/ {
            alias /home/myapp/public/;
    }

    location / {
            proxy_pass http://IPADRESSOFNODEJSSERVER:8080;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            #try_files $uri $uri/ =404;
    }

Every request with /public/ at the first part of the url will be handled by nginx and every other request will be proxied to you nodejs app at your IPADRESSOFNODEJSSERVER:NODEJSPORT usually the IPADRESSOFNODEJSSERVER is the localhost 网址开头部分带有/ public /的每个请求都将由nginx处理,其他每个请求都将通过IPADRESSOFNODEJSSERVER:NODEJSPORT代理给您的nodejs应用IPADRESSOFNODEJSSERVER:NODEJSPORT通常, IPADRESSOFNODEJSSERVER是本地主机

The second option using NGNIX refers to the doc section 使用NGNIX的第二个选项参考doc部分

An even better option is to use a reverse proxy to serve static files; 更好的选择是使用反向代理来提供静态文件。 see Use a reverse proxy for more information. 有关更多信息,请参见使用反向代理。

http://expressjs.com/en/advanced/best-practice-performance.html#proxy http://expressjs.com/en/advanced/best-practice-performance.html#proxy

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

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