快速提供静态文件:发送标头后无法设置标头

Rcr*_*rab 1 node.js express

我正在使用express.static内置的中间件函数来提供静态文件,但是控制台会打印错误:发送后无法设置标头

这是我的代码,我不知道这是怎么了

'use strict';

let path = require('path');
let express = require('express');
let bodyParser = require('body-parser');
let mongoose = require('mongoose');
let formidable = require('express-formidable');
let routes = require('./routes');
let app = express();
let port = process.env.PORT || 3000;


let db = mongoose.connect('mongodb://localhost:27017/old-driver');

// deal with img post
app.use(formidable({
  uploadDir: path.join(__dirname, 'upload'),
  keepExtensions: true
}));

app.use(bodyParser.urlencoded({extended: true })); 
app.use(bodyParser.json());

// access-control
app.all('*', (req, res, next) => {
  res.set("Access-Control-Allow-Origin", "*");
  res.set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
  res.set("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.set("X-Powered-By",' 3.2.1')
  res.type("application/json");
  res.type("jpg");
  next();
});


// set assets path, GET /assets/demo.png
app.use('/assets', express.static('upload'));

routes(app);

app.listen(port);
Run Code Online (Sandbox Code Playgroud)

路由中间件方法:

getAllTeachers: (req, res) => {
    Teacher.find({}, (err, teacher) => {
        if (err) {
            res.send(err);
        } else {
            res.json(teacher);
        }
    });
},
Run Code Online (Sandbox Code Playgroud)

即使我删除了如下所示的访问控制代码,仍然会出错

let db = mongoose.connect('mongodb://localhost:27017/old-driver');

// deal with img post
app.use(formidable({
  uploadDir: path.join(__dirname, 'upload'),
  keepExtensions: true
}));

app.use(bodyParser.urlencoded({extended: true })); 
app.use(bodyParser.json());

// set assets path, GET /assets/demo.png
app.use('/assets', express.static('upload'));

routes(app);

app.listen(port);
Run Code Online (Sandbox Code Playgroud)

当我请求2个现有jpg文件时,我在一个jpg文件404屏幕截图中也遇到404错误

Zeu*_*eus 6

为了解决这个问题,您需要使用app.use功能而不是app.all安装中间件功能。app.use将中间件功能添加到中间件堆栈中,以确保以添加它们的相同顺序执行它们。

因此,您需要这样做:

app.use((req, res, next) => { //change app.all to app.use here and remove '*', i.e. the first parameter part
  res.set("Access-Control-Allow-Origin", "*");
  res.set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
  res.set("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.set("X-Powered-By",' 3.2.1')
  res.type("application/json");
  res.type("jpg");
  next();
});
Run Code Online (Sandbox Code Playgroud)

编辑:

正如您在评论中告诉的那样,上述方法不起作用,因此您可以做的是express.static在提供文件之前使用setHeaders方法设置标头,如下所示:

app.use('/assets', express.static('upload', {
  setHeaders: function(res, path) {
    res.set("Access-Control-Allow-Origin", "*");
    res.set("Access-Control-Allow-Headers", "Content-Type,X-Requested-With");
    res.set("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.set("X-Powered-By",' 3.2.1')
    res.type("application/json");
    res.type("jpg");
  }
}));
Run Code Online (Sandbox Code Playgroud)

将此静态文件服务中间件方法放在上方app.use以便不会调用用于设置标头的app.use方法,因此不会再次设置标头。