为什么sendFile方法显示源代码而不是html页面

时间:2021-08-21 15:49:37

I encountered a new problem in the nodejs.
The following code is a demo of the express framework.
I want to return a html page, but returned to the page's source code.
Why browser does not parse pages? Please help me!

我在nodejs中遇到了一个新问题。下面的代码是express框架的演示。我想返回一个html页面,但是返回到页面的源代码。为什么浏览器不解析页面?请帮助我!

var express = require('express');
var path = require('path');
var router = express.Router();

router.get('/', function (req, res, next) {
    res.sendFile(path.join(__dirname, '../views/index.html'));
});

1 个解决方案

#1


3  

can you try this

你能试试这个

app.use(express.static('../views'));

instead of

而不是

router.get('/', function (req, res, next) {
    res.sendFile(path.join(__dirname, '../views/index.html'));
});

extra edit: you can make this index.html static , then you can edit it via Angular etc. this would be a better solution.

额外编辑:你可以建立这个索引。html静态,然后你可以通过角等编辑它这将是一个更好的解决方案。

#1


3  

can you try this

你能试试这个

app.use(express.static('../views'));

instead of

而不是

router.get('/', function (req, res, next) {
    res.sendFile(path.join(__dirname, '../views/index.html'));
});

extra edit: you can make this index.html static , then you can edit it via Angular etc. this would be a better solution.

额外编辑:你可以建立这个索引。html静态,然后你可以通过角等编辑它这将是一个更好的解决方案。