访问html返回纯文本,为什么express,get的res.sendFile返回的是html纯文本?

let express = require('express');

let db = require('./db/db');

let app = express();

app.all('*', function (req, res, next) {

res.header("Access-Control-Allow-Origin", "*");

res.header("Access-Control-Allow-Headers", "X-Requested-With");

res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");

res.header("X-Powered-By", ' 3.2.1')

res.header("Content-Type", "application/json;charset=utf-8");

next();

});

app.get('/', (req, res) => {

res.sendFile(__dirname + '/index.html');

});

let apiRoutes = express.Router();

apiRoutes.get('/seller', function (req, res) {

db.find(req.query.id, function (err, result) {

if (err) {

console.log('Error:' + err);

return;

} else {

let seller = result[0].toObject();

delete seller.goods;

delete seller.ratings;

res.json({

errno: 0,

data: seller

});

}

});

});

apiRoutes.get('/goods', function (req, res) {

db.find(req.query.id, function (err, result) {

if (err) {

console.log('Error:' + err);

return;

} else {

res.json({

errno: 0,

data: result[0].toObject().goods

});

}

});

});

apiRoutes.get('/ratings', function (req, res) {

db.find(req.query.id, function (err, result) {

if (err) {

console.log('Error:' + err);

return;

} else {

res.json({

errno: 0,

data: result[0].toObject().ratings

});

}

});

});

app.use('/api', apiRoutes);

app.listen(3000);

console.log('Listening on port 3000...');

为什么

app.get('/', (req, res) => {

res.sendFile(__dirname + '/index.html');

});

返回的是html纯文本而不是文件?

bVNMp7?w=1348&h=784

但是

var express = require('express');

var bodyParser = require('body-parser');

var db = require('./validate');

var app = express();

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

// respond with "hello world" when a GET request is made to the homepage

app.get('/', (req, res) => {

res.sendFile(__dirname + '/index.html');

});

app.post('/dosomething', (req, res) => {

var message = req.body;

db.create(message.name, message.age, message.city, function (err, mes) {

if (err) {

throw err;

} else {

console.log(mes);

}

});

})

app.listen(1212);

console.log('is ok ...');

就能正常显示网页?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值