简体   繁体   中英

Sending Status Codes in NodeJS when serving static content

I am building a simple server to serve static content for a webpage. I want to send status codes back but when I try to create a function for the app.use() the content is not served.

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

app.listen(3000, function() {
  console.log("We are now listening on Port:3000")

  app.use(express.static('public'));

  app.use('*', express.static('public/404.html'));

});

The content is served when I run this, but I cannot get the status codes as I need. Any help is appreciated.

I don't know exactly what you want. This is my suggest solution:

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

    app.listen(3000, function() {
      console.log("We are now listening on Port:3000")

      app.use(express.static('public'));

      app.use('*', function(req, res) {
        // change your status code here
        res.status(404).sendFile(__dirname + "/public/404.html");
      });

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM