node.js - How are Status Codes Added to Express

user15845993

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

Marita Farruggia

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related