Express.js: How to create a server
express.js allows us to create a server with only a few lines of code.
- Install Express.js
$ npm i express
- import express
- call the top-level express() function
- 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
Result of Console
We can use any HTTP request methods (GET, POST, PUT, DELETE...)