0

I am developing an Express web application with Node JS on Google App Engine Flexible environment (Managed VMs). Basically I only have one static HTML page that I need to serve since everything is loaded dynamically in 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.

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.

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'));
// ...
});
3
  • In production, you could use an nginx server to serve you static file and let you node.js server handle the dynamic content. 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. Commented Apr 5, 2016 at 9:50
  • The problem is that I am running on App Engine Flexible and I don't know if I have access to nginx configuration. Commented Apr 5, 2016 at 11:53
  • Never used it but it seems that you can after quick google search it seems that it is possible to install your own nginx cloud.google.com/appengine/docs/flexible Infrastructure Customization and cloud.google.com/solutions/https-load-balancing-nginx but the default express middleware for serving static files will do the trick also app.use('/public', express.static(path.join(__dirname, './public'))); Commented Apr 5, 2016 at 12:01

1 Answer 1

1

To serve a static file you can define a static folder content in 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

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. 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.

An nginx configuration could be

   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

The second option using NGNIX refers to the doc section

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

Sign up to request clarification or add additional context in comments.

2 Comments

I know about the express.static function, what I don't know however is how to replace res.sendFile with the the appropriate function in "static-serve" package.
This is the correct answer, you should put your angular app into a folder and then serve that folder statically with express. This way angular does its thing in the front and express can handle api calls, persistent login etc. This is the way I am builing my app MEANBoilerPlate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.