Express.js: How to create a server

ยท

1 min read

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