Static routing in node express - node.js

Im using a set of static routes in Node Express and are experiencing a very strange phenomenon.
The routing is set up so that '/list/*' and '/setup/*' gets to different html-files where the directories are used kind of as storage id, for example updating info on the page url/setup/12345 would store info in 12345.
To be able to load scripts and such there is also route with regex matching /assets/ to allow url/setup/assets/script.js to be reached without routing to setup.html with a new storage id.
My problem is that this works for url/setup/assets/script.js but not for url/list/assets/script.js even though they have identical routings.
Edit:
Navigating to url/list/assets/script.js leads to list.html (unwanted behaviour)
Navigating to url/setup/assets/script.js leads to script.js (wanted behaviour)
Any ideas on why '/list/*'` wont work?
Here are my static routes:
app.use(/assets/, express.static(wwwPath));
app.use('/list/*', function(req, res, next) {
res.sendFile('list.html', { root: wwwPath });
});
app.use('/setup/*', function(req, res, next) {
res.sendFile('setup.html', { root: wwwPath });
});

The solution was to use custom middleware. Here are the new routes:
var requestParser = function(req, res, next) {
if(req.originalUrl.indexOf('/assets/') >= 0) {
var assetPath = path.join(wwwPath, req.path.slice(req.url.indexOf('/assets/')));
fs.stat(assetPath, function(error, stat){
if(stat && stat.isFile()) {
res.sendFile(assetPath);
}
else{
res.status(404).send('<h1>404</h1>');
}
});
}
else {
next();
}
};
app.use(requestParser);
app.use('/list/*', function(req, res, next) {
res.sendFile('schema.html', { root: wwwPath });
});
app.use('/setup/*', function(req, res, next) {
res.sendFile('setup.html', { root: wwwPath });
});

Related

How to routing for mobile view in express

I want to change the URL for mobile. www.example.com to m.example.com
I use isMobile function to detect device type.
I do not know what to do for routing.
for example: m.example.com/index
please help me
in app.js
function isMobile(req, res, next) {
if ((/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(req.headers["user-agent"]))) {
res.redirect('//m.' + req.host+ req.path);
} else {
next()
}
}
app.get('/',isMobile,routes.index);
If device is a mobile,i want to run routes.mobileindex. How?
If you want to render a separate view for mobile devices in the same application then it would be pretty straigh forward. it is a just a matter of creating another view file and you can apply your separate layout and CSS style to design it.
var express = require("express");
var router = express.Router();
function isMobile(req, res, next) {
if (
/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(
req.headers["user-agent"]
)
) {
// Instead of redirecting to another view you can also render a separate
// view for mobile view e.g. res.render('mobileview');
res.redirect("/mobile/device");
} else {
next();
}
}
/* GET home page. */
router.get("/", function(req, res, next) {
res.render("index", {
title: "Express"
});
});
router.get("/device", isMobile, function(req, res, next) {
res.json("normal view is rendered");
});
router.get("/mobile/device", function(req, res, next) {
res.render("mobile");
});
module.exports = router;
I have set a redirection here but this is not the best way if you are in the same application, because you can directly hit /mobile/device view, in that case you also need a middleware or can reuse the isMobile middleware to redirect to normal view. I would rather suggest to use res.render('yourviewname')
Hope this gives you an idea to proceed futher!

node js - web page sometimes raise 404 error

my web site sometimes raise 404 error even though there is correct path in my index file.
When occurs 404 error, I reload the page and the page correctly uploaded.
even though by reloading the page successfully page uploaded, I think it's not stable...
Do you have any idea? I don't know why it happens.
This is my route and handler to main page!
And I used vhost to connect domain
app1.get('/',(req,res)=>{
var arr = poplist;
var type = 'recommended';
var session = null;
if(req.session.user){
session = req.session;
}
if (req.session.series_id){ /*5*/
var search_car = req.session.series_id;
arr.unshift({'id':req.session.series_id,'series_name':req.session.manu_name + ' ' + req.session.series_name});
type = 'mycar';
} else {
var search_car = recommend_car(arr).id; /*4*/
}
get_recommend_container(arr,function(err,recommend_container){
if (err) { res.send('err') }
else {
get_best_reviews_container(function(err,best_reviews_container){
if (err) { res.send('err') }
else {
/*search_car for query in menubar*/
res.render('main', {'type':type,'poplist':poplist,'recommend_container':recommend_container,'best_reviews_container':best_reviews_container,'search_car':search_car,'session':session});
}
});
}
});
});
....
app.use(vhost('mysite.com', app1));
app.use(function(req, res, next) {
res.status(404).send('Sorry cannot find the page!');
});
app.listen(8000,'myip',function(){
console.log("Connected to 8000 port!..")
});
You explicitely define a "404 middleware" :
app.use(function(req, res, next) {
res.status(404).send('Sorry cannot find the page!');
});
This means that every time your app will receive any request, it will execute that middleware that will send back a 404.
Documentation : https://expressjs.com/en/guide/using-middleware.html
This example shows a middleware function with no mount path. The
function is executed every time the app receives a request.
var app = express()
app.use(function (req, res, next) {
console.log('Time:', Date.now())
next()
})

Expressjs - Get req.route in a middleware [duplicate]

Does anyone know if it's possible to get the path used to trigger the route?
For example, let's say I have this:
app.get('/user/:id', function(req, res) {});
With the following simple middleware being used
function(req, res, next) {
req.?
});
I'd want to be able to get /user/:id within the middleware, this is not req.url.
What you want is req.route.path.
For example:
app.get('/user/:id?', function(req, res){
console.log(req.route);
});
// outputs something like
{ path: '/user/:id?',
method: 'get',
callbacks: [ [Function] ],
keys: [ { name: 'id', optional: true } ],
regexp: /^\/user(?:\/([^\/]+?))?\/?$/i,
params: [ id: '12' ] }
http://expressjs.com/api.html#req.route
EDIT:
As explained in the comments, getting req.route in a middleware is difficult/hacky. The router middleware is the one that populates the req.route object, and it probably is in a lower level than the middleware you're developing.
This way, getting req.route is only possible if you hook into the router middleware to parse the req for you before it's executed by Express itself.
FWIW, two other options:
// this will only be called *after* the request has been handled
app.use(function(req, res, next) {
res.on('finish', function() {
console.log('R', req.route);
});
next();
});
// use the middleware on specific requests only
var middleware = function(req, res, next) {
console.log('R', req.route);
next();
};
app.get('/user/:id?', middleware, function(req, res) { ... });
I know this is a little late, but for later Express/Node setups req.originalUrl works just fine!
Hope this helps
This nasty trick using prototype override will help
"use strict"
var Route = require("express").Route;
module.exports = function () {
let defaultImplementation = Route.prototype.dispatch;
Route.prototype.dispatch = function handle(req, res, next) {
someMethod(req, res); //req.route is available here
defaultImplementation.call(this, req, res, next);
};
};
req.route.path will work to get the path for the given route. But if you want the complete path including the path of the parent route, use something like
let full_path = req.baseUrl+req.route.path;
Hope it helps
You can take a look at Router().stack, which has all the routes defined. In your middleware you need to manually compare the available routes with the one called to define actions.

forwarding to another route handler without redirecting in express

I have the following code :
app.get('/payment', function(req, res) {
// do lots of stuff
});
now I want to add the following :
app.post('/payment', function(req, res) {
req.myvar = 'put something here';
// now do the same as app.get() above
});
Obviously I want to reuse the code. I tried doing next('/payment') inside the post handler and put it above the get handler, but no luck, probably because they are different VERBs.
What are my options ?
Thanks.
Just lift out the middleware to its own function and use it in both routes.
function doLotsOfStuff (req, res) {
// do lots of stuff
}
app.get('/payment', doLotsOfStuff);
app.post('/payment', function(req, res, next) {
req.myvar = 'put something here';
next();
}, doLotsOfStuff);

node.js expressjs pattern match not equal

I'm using expressjs with node and running both https and http.
I want to require that all routes for /secure/* use https. This is done:
app.all("/secure/*", function(req, res, next) {
if (!req.connection.encrypted) {
res.redirect("https://" + req.headers["host"].replace(new RegExp(config.http_port, "g"), config.https_port) + req.url);
} else {
return next();
};
});
However, I also want to require that all routes that are not using /secure/* and try to access https, are redirected using the same method to http.
I tried doing this:
app.all("*", function(req, res, next) {
console.log(req);
if (req.connection.encrypted) {
res.redirect("http://" + req.headers["host"].replace(new RegExp(config.https_port, "g"), config.http_port) + req.url);
} else {
return next();
};
});
But I end up in a redirect loop when accessing the https pages. Is there a way to specify all routes, except those with /secure/* ?
Thank you!
A simple solution to your problem is:
app.all("*", function(req, res, next) {
if (req.connection.encrypted && !/^\/secure/.test(req.url)) {
res.redirect("http://" + req.headers["host"].replace(new RegExp(config.https_port, "g"), config.http_port) + req.url);
} else {
return next();
};
});
Only do the redirect if the URL doesn't start with /secure.
However, I'd propose that instead of the redundant 'secure' label in the URLs, just mark certain paths as requireHTTP or requireHTTPS. You know you can pass multiple methods into app.get and other such router methods, right? Assuming you define requireHTTP and requireHTTPS (which would be identical to your original functions), you'd just do:
app.get("/path/to/keep/encrypted", requireHTTPS, function(req, res) {
// Rest of controller
});
app.get("/path/to/keep/public", requireHTTP, function(req, res) {
// Rest of controller
});
app.get("/path/you/dont/care/about/encryption/status", function(req, res) {
// Rest of controller
});
That should do it.

Resources