Node.js + EJS + smws : Multi language support in web app - node.js

Currently, I'm building a web app with Node.js with an EJS view engine and SMWS for multilanguage support.
const express = require("express")
const app = express()
const smws = require('smws'); // Multilanguage
// Implementing the rate limiter
const rate_limiter = require("./middlewares/rateLimiter")
const rateLimiterForAPI = rate_limiter.rateLimiterForAPI;
const rateLimiterForWeb = rate_limiter.rateLimiterForWeb;
// Signup page
app.get(smws.split('/:lang/register'), rateLimiterForWeb, (req, res) => {
smws.run(req,res,{
page: 'register',
useParams: ['lang'],
page404: '404'
});
})
My web app shows the page with the proper language content from the language files en.json and it.json.
My question:
When the page loads, I need to do perform some processes and pass the result values to the templates (index.ejs). But I couldn't find the proper documentation from https://www.npmjs.com/package/smws about how to pass the custom parameters to the template files.
Can anyone guide me with this npm package? my main objective is to adapt the multi-language therefore kindly suggest to me the best npm package which I can use instead of SMWS.
Thanks in advance.

Related

Nodjs swagger auto gen integration

I need to develop an API using NodeJS and also need to develop documentation for API also. I integrated with swagger auto-gen for swagger.json creation. But the swagger.json not generating properly if I used routes.js as below
var express = require('express');
module.exports = function(app) {
var userController = require('../controller/userController');
var apiRouter = express.Router();
var routerV1 = express.Router();
var routerV2 = express.Router();
app.use('/admin', apiRouter);
apiRouter.use("/v1", routerV1);
apiRouter.use("/v2", routerV2);
routerV1.route('/users').get(userController.getUsersV1);
routerV2.route('/users').get(userController.getUsersV2);
}
and also mapped these routes.js in swagger.js
Please suggest the best way to generate swagger.js
Do we need to create routes file for all controller?
Version 2 of swagger-autogen added this feature. Previous versions don't recognize routes. In your case, the best way to generate the file swagger.js is:
file: swagger.js
const swaggerAutogen = require('swagger-autogen')();
const outputFile = './swagger_output.json';
const endpointsFiles = ['./routes.js']; // root file where the route starts.
swaggerAutogen(outputFile, endpointsFiles).then(() => {
require('./index.js'); // Your project's root file
})
Update your module to the latest version.
And run your project with: node swagger.js
And about the routes file for all controller, you don't need to implement it for each one, but it also depends on the structure of your code. If you have a root route file like the example, this is enough for all sub-routes to be scanned. I hope it helps you. Take a look at this example if you need to:
swagger-autogen using router

Get my Action’s server URL in (JavaScript) fulfilment code

I am using actionssdk and I build my Action fulfilments using Javascript and node.js + Express.
I am looking for a way to get the url (protocol + host name + port) of the server where the fulfilment is hosted.
Is there a simple way to do this? E.g. in the MAIN intent? Is there some conv-property I can use? Can I get hold of a req-parameter in the MAIN-intent, from which I can deduct hostname etc?
const express = require('express');
const expressApp = express();
const { actionssdk, ... } = require('actions-on-google');
const app = actionssdk({
ordersv3: true,
clientId: ...
});
expressApp.post('/fulfilment', app);
app.intent('actions.intent.MAIN', (conv) => {
let myUrl: string = ... // ???????
...
});
(background: obviously I know myself to where I deployed my fulfilment code. But I have a reusable template for fulfilment code in which I want to refer to the host url, and I do not want to type that in manually each time I develop a new Action).
You can get access to the request object in a middleware via Framework Metadata which is by default of type BuiltinFrameworkMetadata which contains objects used by Express
For example, you can use it like this, which will be ran before each request:
app.middleware((conv, framework) => {
console.log(framework.express.request.headers.host)
})

Understrap 'require is not defined'

I'm using the Understrap theme for my Wordpress project. I created a simple custom .js file to add to my project but I'm getting a 'require is not defined' error.
custom .js file
var express = require('express');
var moment = require('moment');
var app = express();
var m = moment();
app.get('/', function (req, res) {
m.set({'year': 2017, 'month': 5, 'day': 29});
//res.send(moment().format('YYYY/M/D'));
var today = document.getElementById('today');
today.innerHTML = moment().format('YYYY/M/D');
})
Nodejs is a server side language, you cannot use require in browser side unless you use browserify or webpack. And also you cannot use express in browser because it's a server side framework, as you are using a theme for wordpress, I don't think you need to use nodejs at all, just write the plain js.

Is there a way to render a NodeJs Express view to a variable?

Is there a way to render an express view to a variable as opposed to the response stream?
var view = path.join( __dirname, '/../customer-product/views/copysheet.html');
res.render( view, {
data: product
})
I need the html on the server side so that I can be passed to PhantomJs for PDF generation.
There's a pretty good article on it at Strongloop. The upshot is that you would interact directly with the view engine like so:
var templatePath = require.resolve('./copysheet.html');
var templateFn = require('jade').compileFile(templatePath); // or whatever view engine you're usingg
var output = templateFn({data:product});

Extend View class in Express on Node.js

I'd like to override the View class in the Express framework, used in Node.js. I want to augment the lookup method, but I can't see a way to do this without altering the Express and App modules. I'd favour deriving from the Express framework, but I can't figure out a neat way to do this.
Any ideas?
Thanks
It seems to me you should be able to:
var View = require('express/lib/view');
// Keep reference to original lookup method
var _lookup = View.prototype.lookup;
// Override lookup method
View.prototype.lookup = function (path) {
// Your implementation here
};
Update:
Run this as a demonstration:
var View = require('express/lib/view');
var _lookup = View.prototype.lookup;
var express = require('express');
View.prototype.lookup = function (path) {
console.log('LOOKUP!!! ' + path);
return _lookup.call(this, path);
};
var app = express();
app.get('/', function (req, res) {
res.render('foo.jade');
});
app.listen(3000);
Run
node app & sleep 1 && curl localhost:3000
I hope this will demonstrate the viability of this way of overriding a method.
It depends on which version of Express you are using.
You can easily augment the view lookup code only if your app is using Express prior to version 3
Since Express 3.0 that's not doable anymore.
You can check one of my old related answers for sample code:
Multiple View paths on Node.js + Express

Resources