Express.js multilanguage with i18n-node - javascript

How to call controller with express.js routing?
app.get('*', function(req, res, next) {
var regExp = /^\/([a-z]{2})(\/|$)/gi,
exec = regExp.exec(req.url);
exec = exec != null ? exec[1] : undefined;
if(exec == undefined) {
// add language prefix to link
}
else {
i18n.setLocale(exec);
// add language prefix to link
}
next();
});
If I open the page /about I need to get url like this: http://example.com/en/about. How I can do it and how to display call 'about' controller?
app.get('/about', function(req, res) {
console.log('Here is about');
res.send('Hello, World');
});
Not suitable: app.get('(en|de|ru)/about', ...)
Thanks in advance.

You could do something like
app.get('/:language/about', function(req, res) {
var language = req.params.language;
i18n.setLocale(language);
});
For multiple routes you could maybe do this
controllers.js
module.exports = {
about: function(req, res) {
res.send('about');
}
}
app.js
var controllers = require('./controllers');
app.all('/:language/:controller', function(req, res) {
i18n.setLocale(req.params.language);
controllers[req.params.controller](req, res);
});

The following works for me very well, so the language code can be optional:
var i18n = require('i18n');
server.use(express.static(__dirname + '/client/www'));
server.use('/en', express.static(__dirname + '/client/www'));
server.use('/zh', express.static(__dirname + '/client/www'));
function regexPath(p) {
return new RegExp('(?:/(en|zh))?' + p, 'i');
}
server.use(i18n.init);
server.all('*', function (req, res, next) {
var l = /^\/(en|zh)/i;
if (l.test(req.url)) {
var a = l.exec(req.url);
var local = a[1];
i18n.setLocale(local);
res.setLocale(local);
} else {
i18n.setLocale('zh');
res.setLocale('zh');
}
next();
});
server.get(regexPath('/signin'), function (req, res) {
res.render('sign-in');
});

Related

Edit and create HTML content using node.js

I want to create html content that looks something like this using node.js.
<div class="outputs">
...
</div>
I have the following code:
var mongoose = require("mongoose");
var express = require("express");
var bodyParser = require("body-parser");
var Url = require("./models/Url");
var shortId = require("shortid");
var http = require("http");
var app = express();
var { JSDOM } = jsdom;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect(process.env.MLAB_URI);
app.get("/urls", (req, res, next) => {
Url.find({}, function(err, data) {
res.json(data);
console.log(data.length);
});
});
app.get("/deletebase", (req, res, next) => {
Url.deleteMany({}, function(err, data) {
res.json(data);
});
});
app.use(express.static(__dirname + "/"));
app.get("/:shortUrl", function(req, res, next) {
Url.findOne({ shortUrl: req.params.shortUrl }, function(err, findUrl) {
if (err) console.log(err);
if (!findUrl) {
return next({ status: 400, message: "unknown shorturl" });
}
res.redirect(findUrl.longUrl);
});
});
app.post("/", function(req, res) {
var url = new Url(req.body);
var hostname = req.headers.host;
var expression = /[-a-zA-Z0-9#:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()#:%_\+.~#?&//=]*)?/gi;
var regex = expression;
if (regex.test(url) === true) {
url.shortUrl = shortId.generate();
url.fullUrl = "https://" + hostname + "/" + url.shortUrl;
url.save(function(err, savedUrl) {
if (err) console.log(err);
res.redirect("https://" + hostname);
});
} else {
res.redirect("https://" + hostname);
}
});
var options = {
runScripts: "dangerously",
resources: "usable"
};
app.listen(3000, function() {
console.log("RUNNING");
});
I want to get length of the data and create that many div objects with longUrl and shortUrl objects in it. Also when database will be updated new div object should be created, and when I delete database information all the div elements should be deleted too, is this possible to do?
You should be using a templating engine for this the two most popular ones for Node.js are pug(formerly Jade) and hbs(Handlebars.js).
There are a lot of other template engines here you could consider.

Express js flow behavior

I need to understand how flow works in an Express app using Routes,
I have These Routes
app.use(require('./routes/reportsRouter'));
app.use(require('./routes/crewsRouter'));
app.use(require('./routes/api'));
app.use(require('./routes/filesRouter'));
Now in ./routes/crewsRouter I have th following Code
var express = require('express');
var router = express.Router();
router.use(function(req, res, next) {
var url = req.url;
//..... Edit URL if Contains // empty parm
// crews//today; correct Url crews/all/today
// this give me a list of all jobs for all crews for today.
console.log("CrewsRouter: ", req.method + ".( " + url + " )");
next();
});
router.get('/crews', function(req, res) {
if (!req.params.key) { next(); }
res.render('crewsView',{
pageTitle:'All-Crews',
pageID:'crews',
crewInfo: {"aka": "all"},
reqOptions: ''
});
});
router.get('/crews/:leadId?/:options?', function(req, res) {....}
module.exports = router;
and in reportsRouter.js
var express = require('express');
var router = express.Router();
router.use(function(req, res, next) {
// log each request to the console
console.log("ReportsRouter: ", req.method + ".( " + req.url + " )");
// continue doing what we were doing and go to the route
next();
});
router.get('/reports', function(req, res) {
//var data = req.app.get('appData')
res.render('reportsView',{
pageTitle:'Reports',
pageID:'reports'
});
});
module.exports = router;
The behavior I'm having is no matter what route I request
both of the route.use is running. Is this normal and what can i do to stop this behavior.
let crewsRouter = require('routes/crewsRouter');
...
app.use('/crews', crewsRouter);
app.use('/reports', reportsRouter);
# crews
...
router.get('/', function(req, res) {
... # this used to be your '/crews' handler
}
# reports
...
router.get('/', function(req, res) {
... # this used to be your '/reports' handler
}
You should probably be doing something like this:
app.use('/reports', require('./routes/reportsRouter'));
app.use('/crews', require('./routes/crewsRouter'));
app.use('/api', require('./routes/api'));
app.use('/files', require('./routes/filesRouter'));
And then in your reportsRouter:
var express = require('express');
var router = express.Router();
router.use(function(req, res, next) {
// log each request to the console
console.log("ReportsRouter: ", req.method + ".( " + req.url + " )");
// continue doing what we were doing and go to the route
next();
});
router.get('/', function(req, res) {
//var data = req.app.get('appData')
res.render('reportsView',{
pageTitle:'Reports',
pageID:'reports'
});
});
module.exports = router;
And your crewsRouter:
var express = require('express');
var router = express.Router();
router.use(function(req, res, next) {
var url = req.url;
//..... Edit URL if Contains // empty parm
// crews//today; correct Url crews/all/today
// this give me a list of all jobs for all crews for today.
console.log("CrewsRouter: ", req.method + ".( " + url + " )");
next();
});
router.get('/', function(req, res) {
if (!req.params.key) { return next(); }
res.render('crewsView',{
pageTitle:'All-Crews',
pageID:'crews',
crewInfo: {"aka": "all"},
reqOptions: ''
});
});
router.get('/:leadId?/:options?', function(req, res) {....});
module.exports = router;

Providing Custom Params In Express Middleware

I am having a problem with my Node.js app. In short I want to pass custom parameters into my middleware function other than just req, res, and next.
Middleware file:
var DB = require('./DB.js');
function requirePermissions(e) {
console.log('nope')
}
module.exports = requirePermissions;
Route:
router.post('/posts', requirePermissions('post_creation'), function(req, res) {
var o = req.body,
title = o.post.title,
content = o.post.content;
res.send('made it');
});
I have confirmed that using function requirePermissions(req, res, next) {} will work, but I do not understand how to include my own parameters.
Your function requirePermissions should return another function which will be the actual middleware:
function requirePermissions(e) {
if (e === 'post_creation') {
return function(req, res, next) {
// the actual middleware
}
} else if (e === 'something_else') {
return function(req, res, next) {
// do something else
}
}
}
You can also do it like that:
function requirePermissions(e) {
return function(req, res, next) {
if ('session' in req) {
if (e === 'post_creation') {
// do something
} else if (e === 'something_else') {
// do something else
}
}
}
}
You can just create an anonymous function for your middleware that lets you call your actual function with some additional arguments:
router.post('/posts', function(req, res, next) {
requirePermissions('post_creation', req, res, next);
}, function(req, res) {
var o = req.body,
title = o.post.title,
content = o.post.content;
res.send('made it');
});
Or, you can use .bind() to preprend arguments:
router.post('/posts', requirePermissions.bind('post_creation'), function(req, res) {
var o = req.body,
title = o.post.title,
content = o.post.content;
res.send('made it');
});
This will call your requirePermissions() functions with four arguments like this:
requirePermissions('post_creation', req, res, next)

How to configure dynamic routes with express.js

I have a route.js which looks like this:
module.exports = function(app) {
app.get('/tip', function(req, res) {
res.render("tip");
});
app.get('/article', function(req, res) {
res.render("article");
});
app.get('/article1', function(req, res) {
res.render("article1");
});
app.get('/article2', function(req, res) {
res.render("article2");
});
app.get('/article3', function(req, res) {
res.render("article3");
});
app.get('/modules/:name', function(req, res) {
var name = req.params.name;
res.render('modules/' + name);
});
app.get('/modules/esaver/:name', function(req, res) {
var name = req.params.name;
res.render('modules/esaver/' + name);
});
};
Considering i have over 200 different routes to create, i would end up with stuff like 'article1', 'article2' etc
and my app.js is like:
var express = require('express')
,http = require('http')
,fs = require('fs')
,path = require('path');
var app = express();
html_templates = __dirname + '/html_templates';
app.set('views', html_templates + '/views');
app.set('view engine', 'jade');
app.use('/Core', express.static(__dirname + '/Core'));
app.listen(3000, function () {
console.log("express has started on port 3000");
});
require('./html_templates/controller/routes.js')(app);
Is there any dynamic way to create this?
I would do the same thing you did for /modules/:name
app.get('/article/:id', function(req , res){
res.render('article' + req.params.id);
});
It would be more meaningful from a rest point of view.
If you cannot do it for any particular reason you might want to do something like:
var articlesEndpoints = ['/article2', '/article3'];
articlesEndpoints.forEach(function(name) {
app.get(name, function(req, res) {
res.render(name);
});
});
Is this what you meant?
Finally got it working..
In cases where I got, article1, article2 etc:
app.get('/:name(article|article2|article3)?', function(req, res) {
var name = req.params.name;
res.render(name);
});
In cases where I got multi level url, I created a custom function:
function geturl(url) {
app.get('/' + url + '/' + ':name', function(req, res){
var name = req.params.name;
res.render(url + '/' + name);
});
};
There are many ways to implement dynamic express routes. It depends to a great extent on the structure you have implemented in your project, here I leave an example of dynamic routes and I hope it will be useful.
RouterService.js
module.exports = (function(myCustomRoutes) {
let express = require('express');
let router = express.Router();
let methods = Object.keys(myCustomRoutes); // getting methods ('get', 'post'... etc)
let routesMethod = null;
let url = null;
for(i in methods) {
routesMethod = Object.keys(myCustomRoutes[methods[i]]);
for(j in routesMethod) {
url = '/' + routesMethod[j];
url += '/:' + myCustomRoutes[methods[i]][routesMethod[j]].params.join('/:');console.log(url);
router[methods[i]](url, myCustomRoutes[methods[i]][routesMethod[j]].controller);
}
}
return router;
})();
CustomRoutes.js
module.exports = (function() {
let routes = {get: {}, post: {}};
let routerService = require('./RouterService');
// GET: /dynamic1
routes.get.dynamic1 = {
params: [],
controller: function(req, res, next) {
res.send('route 1');
}
};
// GET: /dynamic2/:param1
routes.get.dynamic2 = {
params: [':param1'],
controller: function(req, res, next) {
res.send('route 2');
}
};
// POST: /dynamic3/:param1/:param1
routes.post.dynamic3 = {
params: ['param1', 'param2'],
controller: function(req, res, next) {
res.send('route 3');
}
};
/*
* Export a router with paths
* GET: /dynamic1
* GET: /dynamic2/:param1
* POST: /dynamic3/:param1/:param1
**/
return routerService(routes);
})();
app.js
let express = require('express');
let app = express();
/*
* Option 1
* GET: /dynamic1
* GET: /dynamic2/:param1
* POST: /dynamic3/:param1/:param1
**/
app.use(require('CustomRoutes')());
/*
* Option 2
* GET: /api/v1/dynamic1
* GET: /api/v1/dynamic2/:param1
* POST: /api/v1/dynamic3/:param1/:param1
**/
app.use('/api/v1', require('CustomRoutes')());
Here is what I did to create dynamic APIs while I am in control over which API allows access to which methods. To maintain the APIs from now on, you can just edit the APIs array.
const APIs = [
{
route: 'order',
methods: ['get', 'post']
},
{
route: 'item',
methods: ['get']
},
]
APIs.forEach(api => {
api.methods.forEach(method => {
app[method]('/' + api.route, (req, res) => require('./routes/' + api.route)[method](req, res))
})
})
Here are a couple of other solutions:
app.get(^\/article(\d{1,3})?\/?$, function(req, res, next) {
var n;
if (req.params[0])
n = parseInt(req.params[0], 10);
if (!n || (n > 0 && n < 900))
res.render('article' + (n ? n : ''));
else
next();
});
or use app.all for the first solution or use a generic middleware:
app.use(function(req, res, next) {
var m = ^\/article(\d{1,3})?\/?$.exec(req.url);
if (m) {
var n;
if (m[0])
n = parseInt(m[0], 10);
if (!n || (n > 0 && n < 900))
return res.render('article' + (n ? n : ''));
}
next();
});
I create a new module called: jadewalker. It will create router code automatically.
We can simply add a jadewalker comment to your jade Or pug file.
//- jadewalker=/b,/b/:id
doctype html
html
title b.jade
body
p b.jade
p params: #{params.id}
And add this module to our app. That's all.
var app = require('koa')()
var router = require('koa-router')();
router = require('jadewalker')(router, path.join(__dirname, 'views'));
app.use(router.routes());
We can visit our jade file by the URL http://localhost:3000/b/abc. (^∀^)
It's work on my project
routesPath = path.join(__dirname, 'routes');
fs.readdirSync(routesPath).forEach(function(file) {
require(routesPath + '/' + file)(app);
});

Expressjs: How to share route middleware accross routes

I have defined multiple route middleware and want to share them across multiple routes/controllers.
Here is my setup:
app.js requires ./routes/index.js:
// load fs module
var fs = require('fs');
// import routing files
module.exports = function(app){
fs.readdirSync(__dirname).forEach(function(file) {
if (file == "index.js") return;
var name = file.substr(0, file.indexOf('.'));
require('./' + name)(app);
});
};
index.js loads all routes automaticly in the dir. A possible routes file can look like:
module.exports = function(app) {
app.get('/contacts', function(req, res, next) {
// routing stuff
});
};
Now I got route middleware:
function isAuthenticated(req, res, next) {
if (!req.session.authenticated) return next(new Error('user not authenticated'));
};
function loadUser(req, res, next) {
var query = User.findById(req.session.user_id);
query.populate('contacts');
query.exec(function(err, user) {
if (err) return next(err);
req.user = user;
next();
});
}
which I want to use like:
var User = require('../models/user');
module.exports = function(app) {
app.get('/contacts', isAuthenticated, loadUser, function(req, res, next) {
res.json(req.user.contacts);
});
};
I also would like to avoid requiring them accross all routing files.
A possible solution would also be:
// load fs module
var fs = require('fs');
var routeMiddleware = {
loadUser: function(req, res, next) { // logic },
isAuthenticated: function(req, res, next) { // logic },
};
// import routing files
module.exports = function(app){
fs.readdirSync(__dirname).forEach(function(file) {
if (file == "index.js") return;
var name = file.substr(0, file.indexOf('.'));
require('./' + name)(app, routeMiddleware);
});
};
but I think not the best...
Personally I would declare shared middleware in the app, not in the controllers, i.e.:
routes/home.js:
module.exports = function(req, res, next) { \\ Code }
app.js:
app.get('/', thisMiddleware, thatMiddleware, require('./routes/home'))
You can also make a stack (array, not an object):
theseMiddlewares = [thisMiddleware, thatMiddleware]
app.get('/', theseMiddlewares, require('./routes/home'))
And if these middlewares are used on all routes except a few, you can do the following:
theseMiddlewares = function(req, res, next) {
if (req.url.match(some_regex_for_urls_to_skip)) next()
else {
\\ do stuff...
next()
}
}
Now you can app.use(theseMiddlewares) that middleware, or if it needs to happen in a certain order relative to other middleware, you can use app.all('*', theseMiddlewares)

Categories

Resources