node.js - How do I always serve the same file with express? -
is there way can serve same file?
so, if go website.com/ajsdflkasjd still serves same file website.com/asdnw
i using express node.
the file have static html file, not jade file.
by way, reason i'm wanting this, in case wondering, have angularjs app handles routing me. so, need serve 1 page, , take care of rest.
thanks in advance!
new answer
const app= require('express')() // static file serve app.use(express.static(__dirname)) // not found in static files, default 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 html requests `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` angular.js req.url = '/index.html'; next(); } }); // static file serve app.use(express.static(__dirname)) app.listen(3000)
Comments
Post a Comment