Steps to create Node JS application and install required modules
Step 1: Make a separate folder called server via following command.
mkdir server
Step 2: Initialize Node Application in it via given command.
npm init -y
Step 3: Install Express JS via given command.
npm i express
Step 4: Create a file for code via given command.
touch server.js
Example: Implementation of above approach in the server.js file
Javascript
// Server.js const express = require( 'express' ); const app = express(); const PORT = 3000; app.use(express.json()); app.get( "/" , (req, res) => { console.log( "GET Request Successfull!" ); res.send( "Get Req Successfully initiated" ); }) app.get( "/user" , (req, res) => { res.send(`Client is requesting for USER Data`) }) app.get( "*" , (req, res) => { res.send( "Error 404 Invalid Endpoint" ); }) app.listen(PORT, () => { console.log(`Server established at ${PORT}`); }) |
To run the application type the following command in terminal.
node ./server.js
Output: Verify the result using Postman API testing
Understanding the Function of a Wildcard Route in Express.js
In this article, we are going to learn about the Wildcard Route and its functions. Wildcard routes are the default case route. If a client requests a route that does not exist on the server, that particular route will be routed to the wildcard route; it is also known as the Error 404 route.