Home > Mobile >  node.js - How are Status Codes Added to Express
node.js - How are Status Codes Added to Express

Time:11-05

I have this code

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

const homepage = `${__dirname}/views/index.html`;
const errpage = `${__dirname}/views/404.html`;
const maincss = `${__dirname}/views/Assets/css/main.css`;
const mainjs = `${__dirname}/views/Assets/js/script.js`;
const skull = `${__dirname}/craneo.OBJ`

//pages
app.get("/skull.obj", (req, res) => res.sendfile(skull))
app.get("/", (req, res) => res.sendFile(homepage));
app.get("/style.css", (req, res) => res.sendFile(maincss))
app.get("/script.js", (req, res) => res.sendFile(mainjs))
app.get("*", (req, res) => res.sendFile(errpage)).code(404);

app.listen("80", () => {
    console.log('server started');
});

in line 15 i appended app.get("*", (req, res) => res.sendFile(errpage)) with .code(404); i tested this and inspect element said this gave me a code 200 i am not sure what is the problem i followed the instrutions from this answer and i am not sure what the problem with this code is and i new to express

CodePudding user response:

It should use res.status(xxx) instead of res.code(xxx) like so:

app.get("*", (req, res) => res.status(400).sendFile(errpage));
  • Related