Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I always serve the same file with express?

Is there any way I can always serve the same file?

So, if they go to website.com/ajsdflkasjd it still serves the same file as website.com/asdnw

I would like to do this using express with node.

The file I have is a static html file, not a jade file.

By the way, the reason I'm wanting to do this, in case you were wondering, is I have an angularjs app that handles all the routing for me. So, all I need to do is serve that one page, and it will take care of the rest.

Thanks in advance!

like image 226
Zane Hitchcox Avatar asked Aug 19 '14 01:08

Zane Hitchcox


People also ask

How do I serve a file in Express?

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. The root argument specifies the root directory from which to serve static assets. For more information on the options argument, see express.static.

What is serving static files in Express?

Static files are files that clients download as they are from the server. Create a new directory, public. Express, by default does not allow you to serve static files. You need to enable it using the following built-in middleware. app.

How do I serve a static file in node?

In your node application, you can use node-static module to serve static resources. The node-static module is an HTTP static-file server module with built-in caching. First of all, install node-static module using NPM as below. After installing node-static module, you can create static file server in Node.

What does Express static () return?

use() function executes middleware in order. The express. static() middleware returns an HTTP 404 if it can't find a file, so that means you should typically call app.


2 Answers

new answer

const app= require('express')()
     // static file serve
     app.use(express.static(__dirname))
     // not found in static files, so default to index.html
     app.use((req, res) => res.sendFile(`${__dirname}/index.html`))
app.listen(3000)

old answer

var express = require('express');
var bodyParser = require('body-parser')
var path = require('path')
var app = express();
     // url encoding
     app.use(bodyParser.urlencoded({extended:false}));
     // gzip
     // redirect all html requests to `index.html`
     app.use(function (req, res, next) {
         if (path.extname(req.path).length > 0) {
                 // normal static file request
                 next();
             }
         else {
                 // should force return `index.html` for angular.js
                 req.url = '/index.html';
                 next();
             }
     });
     // static file serve
     app.use(express.static(__dirname))
app.listen(3000)
like image 200
Zane Hitchcox Avatar answered Sep 17 '22 13:09

Zane Hitchcox


Below is what I'm using express with angularjs in my project. It will always send index.html unless the browser requests resource files (images, css, js, etc.) which contains extname.

    var express = require('express');
    var app = express();
    app.configure(function () {
        // url encoding
        app.use(express.urlencoded());
        // gzip
        app.use(express.compress());
        // redirect all html requests to `index.html`
        app.use(function (req, res, next) {
            if (path.extname(req.path).length > 0) {
                // normal static file request
                next();
            }
            else {
                // should force return `index.html` for angular.js
                req.url = '/index.html';
                next();
            }
        });
        // static file serve
        app.use(express.static(__dirname));
    });
like image 28
Shaun Xu Avatar answered Sep 19 '22 13:09

Shaun Xu