Zane Hitchcox Zane Hitchcox - 6 months ago 27
AngularJS Question

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 it still serves the same file as

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!


new answer

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

old answer

var express = require('express');
var bodyParser = require('body-parser')
var path = require('path')
var app = express();
     // url encoding
     // 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
         else {
                 // should force return `index.html` for angular.js
                 req.url = '/index.html';
     // static file serve