DEV Community

Cover image for Nodemon & Express
Roger
Roger

Posted on

4 3

Nodemon & Express

👋En este post te voy a mostrar como implementar nodemon a tu aplicación de express, nodemon nos permite refrescar automáticamente nuestra aplicación de express al guardar nuestros cambios.

Si no sabes como crear una aplicación de express puedes ver mi articulo donde te enseño a crear una app de express.
Crea Tu Primer App De Express

Pasos

  • Primero debes iniciar un proyecto con npm init -y:
{
  "name": "nodemon",
  "version": "1.0.0",
  "description": "Utilizar nodemon para actualizar nuestro proyecto",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Roger",
  "license": "ISC"
}
Enter fullscreen mode Exit fullscreen mode
  • Ahora debes instalar las siguientes dependencias.
npm i express
Enter fullscreen mode Exit fullscreen mode

Y creamos una pequeña aplicación de express:

const express = require('express')

const app = express()
const PORT = process.env.PORT || 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(PORT, () => {
  console.log('Server is running on port: ' + PORT)
})
Enter fullscreen mode Exit fullscreen mode
  • Seguido de esto debemos instalar la siguiente dependencia de desarrollo:
npm i -D nodemon
Enter fullscreen mode Exit fullscreen mode

Despues de esto, dentro del package.json, debemos agregar la siguiente linea:

"scripts": {
    "start": "nodemon index.js"
  },
Enter fullscreen mode Exit fullscreen mode

Y en nuestra terminal escribimos:

npm run start
Enter fullscreen mode Exit fullscreen mode

Y veremos algo como esto en nuestra terminal:

> nodemon@1.0.0 start
> nodemon index.js

[nodemon] 2.0.16
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server is running on port: 3000
Enter fullscreen mode Exit fullscreen mode

Esto quiere decir que todo esta bien.
Ahora abrimos nuestro proyecto en el navegador http://localhost:3000/, y veremos el mensaje de bienvenida Hello World!, ahora cambiaremos el mensaje de bienvenida a Nodemon is watching!.

app.get('/', (req, res) => {
  res.send('Nodemon is watching!')
})
Enter fullscreen mode Exit fullscreen mode

Guardamos nuestro proyecto, refrescamos la pagina, y veremos que cambio automáticamente el mensaje de bienvenida.
Y de esta forma es como puedes refrescar automáticamente tu aplicación de express.

Si quieres ver mas tutoriales como este puedes entrar a mi perfil y ahí encontraras una gran variedad de tips y tutoriales.👉Roger

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay