Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Why we don't directly do "app = require('express')"?

Almost in all NodeJS application codes I see the following lines:

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

And the question comes to my mind: why we don't directly do:

 app = require('express');
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you mean

app = require('express')();

Then it's bad practice of integrating/using express. Assume we are going to use express to create a simple api server with routing. so, basically the boilerplate would be -

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

app.use(express.json());

app.get('/', (req, res) => {
   res.json({
      type: 'success',
      message: 'We can send json response because we used json module from express :D'
   });
});

app.listen(8000)

So, here we used json module to parse the response as json data and we did that using built in module of express.

If we haven't declared express globally then we have to call the express function again to use the json module.

For example if we don't declare express globally and want to use router and urlencoded parser module then the code would be like this

const app = require('express')();
const json = require('express').json;
const urlencoded = require('express').urlencoded;

app.use(json());
app.use(urlencoded({extended: true}));

And I think it's not good practice of writing proper code. You have to call and create new variables for every modules you want to use from the express.

Hope you get it! I tried my best to make it clear for you from my knowledge.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!