TheBlackBenzKid TheBlackBenzKid - 2 years ago 74
Node.js Question

How to host an adaptive website in NodeJS and Express?

I am planning to make an ecommerce application however I do not wish to have a responsive site. The desktop site will support tablet and will be some what responsive.

The mobile site will be a dedicated mobile site with a seperate view. I normally use Laravel or a PHP approach to detect the mobile and do server-side rendering based on the device (some people call these adaptive sites or

m dot com
sites)

My question is. Using Express, Node and a JavaScript approach can I achieve this?

All users who visit site.com will either site a mobile code or a desktop code depending on device. We currently do this at namshi.com (See Chrome and change device and the code base changes)

Answer Source

Yeah, of course you can do it. As you do it in PHP or any other language you need to find device type using user agent from the request. Below code uses express-device to render different ejs files.

var express = require('express');
var app = express();

// set the view engine to ejs
app.set('view engine', 'ejs');

var device = require('express-device');
app.use(device.capture());

app.get('/hello',function(req,res) {
   if(req.device.type === 'phone'){
       res.render('phoneEjs');
   } else {
       res.render('desktopEjs');
   }
});

app.listen(3000);
console.log("Listening to Port 3000");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download