How do I always serve the same file with an expression?
Is there a way that I can always serve the same file?
So if they go to website.com/ajsdflkasjd it will still serve the same file as website.com/asdnw
I would like to do this with an expression using node.
The file I have is a static html file, not a jade file.
By the way, the reason why I want to do this is, in case you're wondering, I have an angularjs app that handles all the routing for me. So, all I have to do is serve one page and it will take care of the rest.
Thanks in advance!
source to share
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)
source to share
Below I am using expression with angularjs in my project. It always sends index.html
if the browser doesn't ask for resource files (images, css, js, etc.) that contain 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)); });
source to share
Basic configuration for Express 4:
var express = require('express');
express()
.get(/.*/, function(req, res) {
res.sendFile('index.html', {
root: __dirname
});
})
.listen(8080);
These snippets with GZip, BodyParser, etc. pretty cool, but I find it too complicated if you just want to test a single page application. Of course, you can add all these "crafting materials" when you need them.
More details:
source to share
Here is a simple implementation with ExpressJs to create a virtual host and on every return of index.html
var express = require('express');
var app = express();
var vhost = require('vhost');
// Function to create virtualhost
function createVhost(domain,rootDirectory){
var exp = express();
exp.use(express.static(rootDirectory));
exp.get(/.*/,function(req,res){
res.sendFile('index.html',{root:rootDirectory});
})
app.use(vhost(domain,exp));
}
// Virtual Host to create
createVhost('example.com','/home/[user]/[www]/[example.com]');
createVhost('othersite.com','/home/[user]/[www]/[othersite.com]');
// Start Server
app.listen(80,function(){
console.log('Node server on port 80');
});
Remember:
Add domains to "/ etc / host" (on linux)
127.0.0.1 example.com
127.0.0.1 othersite.com
And run in terminal "app.js" with "sudo" for port 80
~/home/[server]$ sudo node app.js
source to share
You can do it both angular side and node side.
In node, you can do something like this:
res.sendfile('<ur html file path');
In angular if you are using ui-router you can use
$urlRouterProvider.otherwise('/otherwise');
and this otherwise must be defined as well
$stateProvider
.state("otherwise", { url : '/urPage'...})
If you are using ngRoute you can do
$routeProvider.otherwise({redirectTo: '/urPage'});
UPDATE
Since your routers are not configured to display urPage by default, on the server you can have something like:
var app = express.createServer();
app.get('/urPage',function(req,res){
res.sendfile('<ur html page>');
});
source to share