Node.js 表达我如何以 javascript 或其他文件类型发送响应

Node.js Express how do I send response as javascript or other file type

我有读取文件 'example.js' 并将其发送给客户端的代码。

app.get('/mods/example.js', function(req, res) {
    fs.readFile('./mods/example.js',
            {encoding: 'utf-8'},
            (err, data) => {
        if (!err)
        {
            res.send("var example = new Mod();" + data);
        }
    });
});

问题是如何将响应作为 javascript 文件发送?

当我在网络浏览器中打开文件时,它是一个 html 文件而不是 javascript 文件。

提前致谢!

根据 noisypixy 的建议,我使用 res.type 函数将响应类型更改为 javascript。

res.type('.js');
res.send("var john = new Human();");

还有很多其他文件类型,例如 htmljsonpng

API 参考示例代码:http://expressjs.com/en/4x/api.html#res.type