OneCompiler

Getting started with NodeJS- Express framework

1013

Express is one of the most popular web application framework in the NodeJS echosystem.

  • pretty fast
  • Minimalist
  • unopinionated
  • Very flexible

How to install express

npm install express

Hello World server example

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

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

app.listen(port, () => console.log(`Server started on ${port} port`))

You can save the above program as index.js file and run it as node index.js

Basic routing sample code

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

//POST 
app.post('/', function (req, res) {
  res.send('POST request. body:', req.body)
})

//DELETE
app.delete('/:id', function (req, res) {
  res.send('DELETE request for id:'. req.params.id)
})