Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is starting express like this "const app = require('express')()" considered bad practice?

So title says it all. Never seen anyone use this syntax.

const app = require('express')()

I like to keep the main js file lean and have everything in modules so all I have there is 15 lines including comments.

Google didn't help and haven't found an answer here.

Is it bad practice to invoke express as you require it?

like image 955
hymair Avatar asked Feb 10 '17 22:02

hymair


People also ask

What is Const Express require (' Express ') meaning?

Whenever you import a module like const express = require('express') express is a module with functions or objects or variables assigned to it .

Why do we need App Express ()?

For request handling, we can use Middleware. A single language is used for frontend and backend development. Express is fast to link it with databases like MySQL, MongoDB, etc. ​​Express allows dynamic rendering of HTML Pages based on passing arguments to templates.

What is better than express JS?

Fastify is another popular open-source web framework for the Node environment that is primarily famous for its speed, i.e., it is up to 20% faster than Express in almost every request. It provides full encapsulation for plug-ins, parses incoming requests into JSON with faster rendering, and provides quick routing.

What does require Express returns?

It is basically an object with methods that are the Express. These functions can be accessed with app. <function name> , like app. set .


1 Answers

In general, your code should be the simplest and clearest way of reliably meeting your requirements. Any code that follows those simple guidelines will not be considered a bad practice in pretty much anyone's book.

There may be other influences on the desired coding style such as team style guidelines, coding style already present in the file/module/project and certain things you may want to do for testing, debugging or reuse. But, since you don't mention any of those influences, I will assume they are not present here.

So, with that first paragraph in mind, as long as you don't need access to the express module elsewhere in this module, then doing:

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

is indeed the simplest and clearest way of accomplishing your goal and should not be considered a bad practice - in fact it should be considered a good practice.


On the other hand, if you were doing this:

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

....


const mainRouter = require('express').Router();


....

const subRouter = require('express').Router();


....

app.use(require('express').static('public'));

Then, you'd have simpler and less redundant code (and perhaps a little faster since there are fewer function calls) if you loaded the express module into its own variable which you could then use everywhere else in that module:

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

....


const mainRouter = express.Router();


....

const subRouter = express.Router();


....

app.use(express.static('public'));
like image 120
jfriend00 Avatar answered Nov 09 '22 18:11

jfriend00