Express.js modules problems

Audrus
Audrus
Aug 22, 2017 · 2 min read

I have worked hard with Express.js modules. There was a lot of problems.

I18n tanslate module. It doesn’t configure with multiple language files. My project has this folder structure:

resources
— locales
— — en
— — — forms.json
— — lt
— — — forms.json
— — — alerts.json

And I had a problem with i18n, because it doesn’t work with this structure. I install i18next, but it crashed. An error was in wrong forms.json file format. I tried to fix it — checked this file with online checkers, remove-readd BOM, but I had no results. I couldn’t find a salution in google for this error. The error was showed by i18nFsBackend module. I came back to i18n module with a new hope to create multiple language files. But I failed. Then I removed i18next, i18nMiddleware and i18nFsBackend and them reinstalled.

And.. Amazing! It works! Why i18next works, i don’t know. Language file forms.json was good!

module.exports = (i18next, i18nMiddleware, i18nFsBackend) => {
return i18next
.use(i18nMiddleware.LanguageDetector)
.use(i18nFsBackend)
.init({
backend: {
// path where resources get loaded from
loadPath: ‘./resources/locales/{{lng}}/{{ns}}.json’,

// path to post missing resources
addPath: ‘./resources/locales/{{lng}}/{{ns}}.missing.json’,

// jsonIndent to use when storing json files
jsonIndent: 2
},
preload: [‘lt’, ‘en’],
ns: [‘forms’],
defaultNS: ‘forms’,
lowerCaseLng: true,
pageDefaultLanguage: ‘lt’,
load: ‘unspecific’,
fallbackLng: ‘lt’,
cookieName: ‘lng’,
useCookie: false,
fallbackToDefaultNS: true,
saveMissing: true,
debug: false,
detection: {
order: [‘querystring’, ‘cookie’],
lookupCookie: ‘language’,
lookupSession: ‘lng’,
lookupPath: ‘lng’,
}

}, (err, t) => {
if (err) return console.log(‘something went wrong loading’, err);
});
};

I installed nodemon for watch files changes. After change a file, nodemon restarts nodejs server.

Csurf module for a validation token. I had error — csurf couldn’t post my form data. Error: invalid csrf token. I searched a salution for 4 days, but I didn’t find. I tried a lot of ways… Finaly in the fifth day I found salution! Problem is with nodemon aplication:

nodemon app.js — csrf invalid
node app.js — works
forever app.js — works

I don’t know, why does nodemon have problem with csurf.

For form parsing I changed body-parser to formidable module.

In route.js file end I created 404 error midleware.

app.use(function(req, res, next) {
res.status(404).render(‘404’);
});

And next I had new error: headers already sent. I changed route.js to simple routes, got a lot of information from Internet, but that didn’t work. Finaly formidable I changed to body-parser and this error gone! But this system have to work with form files. I installed express-busboy and removed body-parser module — system works good!

Node version was 8.1.4, and I tried to update. Node was installed as npm package, but now I tried update through nvm (manager). Node was in /usr/bin/npm… So I new Node version installed in /home/my_project catalog.

)
Audrus

Written by

Developer: Spring, Nodejs, Reactjs.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade