我想向/ privacy页面添加第二条路由,但是它不起作用。我收到“无法获取/privacy.html”错误。有什么建议吗?
此代码不显示privacy.html内容
app.get('/privacy', function(req, res) {
res.sendFile(__dirname + "/privacy.html");
});
这是我的app.js代码
//jshuint esversion: 6
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const https = require("https");
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function(req, res) {
res.sendFile(__dirname + "/signup.html");
});
app.get('/privacy', function(req, res) {
res.sendFile(__dirname + "/privacy.html");
});
app.post("/", function(req, res) {
const firstName = req.body.firstName;
const lastName = req.body.lastName;
const emailAddress = req.body.emailAddress;
const data = {
members: [
{
email_address: emailAddress,
status: "subscribed",
merge_fields: {
FNAME: firstName,
LNAME: lastName
}
}
]
};
const jsonData = JSON.stringify(data);
const url = "https://us.api.mailchimp.com/3.0/lists/bb80b745a8"
const options = {
method: "POST",
auth: "meshiaR:93d648-u"
};
const request = https.request(url, options, function(response) {
if (response.statusCode === 200) {
res.sendFile(__dirname + "/success.html");
} else {
res.sendFile(__dirname + "/failure.html");
}
response.on("data", function(data){
console.log(JSON.parse(data));
});
});
request.write(jsonData);
request.end();
});
// app.post("/privacy", function(req, res){
// res.sendFile(__dirname + "/privacy.html");
// });
app.post("/failure", function(req, res) {
res.redirect("/");
});
app.listen(process.env.PORT || 3000, function(){
console.log("Server is now running on port 3000");
});
答案 0 :(得分:0)
这就是我完成这类任务的方式。
var path = require('path');
app.get('/privacy', function(req, res) {
res.sendFile(path.join(__dirname + '/privacy.html'));
});
顺便说一句,检查您的privacy.html
是否在正确的路径中可用。