OneCompiler

Sending a 404 response code in NodeJS API

197

404 http response code is used in cases where the requested resource not found. It's always good practice to communicate the not found messages in your website using a 404 code. Following code shows you how to send a 404 status code in response code along with the error message.

const express = require('express');
const server = express();

server.get('/ping', (req, res) => {
    res.send("pong!");
});

server.get('/foo', (req, res) => {
    res.status(404).send("Nothing found here");
});

server.listen(5000, () => console.log("server started on 5000"));