Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
Why not using "require('express')()"

To create a simple Web server with NodeJS and Express, all the tutorials giving examples like this

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

app.listen(3000, () => console.log("Started"))

app.get('/', (req, res) =>{
    res.send("Yaaa")
})

My question is, Why not write it like this?

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

app.listen(3000, () => console.log("Started"))

app.get('/', (req, res) =>{
    res.send("Yaaa")
})

The only difference is merging lines 1 and 2, as far as I'm not going to use/need the "express" constant anymore.

Is that wrong? And why?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As far as I know about express framework. Express exposes us to a lot of useful functions. If we don't require or import express in our application then, Our application will not be able to use those functions.
For example if you are creating some REST APIs then, We need our APIs to take form-data or raw as input. If we want to allow our application to take raw-json as input then, we need to add a middleware which consumes a built-in express function.

app.use(express.json())

If you want create an application that has a seperate folder for all the routes. Then, we use express.Routes() for that. That's how we create routes file in seperate routes folder:

import express from 'express';
import userController from 'path/to/user/controller';

const router = express.Router();

router.post('/follow/:userid/:following', helper.verifyToken, userController.follow);

router.get('/someRoute', userController.someAction);

export default router;

Similarly, If we want to serve some static HTML or some react-build. Then, we use express.static() inside app.use() middleware like this:

app.use(express.static('/path/to/static/folder'));
about 4 years ago · Juan Pablo Isaza Relatório

0

As long as you don't need access to the express module elsewhere in this file, then doing:

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

is the best way.

But if we require to use to this express module again and again. Such as below

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

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

Then it becomes a problem and you have require it again and again.

So to make our code less redundant, we use normal given approach. As in below code:

const express = require('express');
const app = express();
const friendRouter = express.Router();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda