Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda