Express.js: How to create a server

express.js allows us to create a server with only a few lines of code.

  1. Install Express.js $ npm i express
  2. import express
  3. call the top-level express() function
  4. use GET request method
    • app.get(path, callback [, callback ...])
    • specify path and callback function

Simple Example

import express from 'express';

const app = express();
const PORT = 4000;

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

const handleListening = () => {
    console.log(`✅ server is listening on http://localhost:${PORT}`);
};

app.listen(PORT, handleListening);

Result of Browser

image.png

Result of Console

image.png

We can use any HTTP request methods (GET, POST, PUT, DELETE...)

Learn more about Basic Routing

No Comments Yet