node.js - 快速路由返回 404 错误
问题描述
我的节点应用程序中的快速路由返回 404 错误。它在本地工作,但在生产服务器上返回 404 错误(请记住,我在上传之前将 AJAX url 更改为生产服务器上的那个)。我的代码:
server.js:
const path = require('path');
const http = require('http');
const express = require('express');
const publicPath = path.join(__dirname, '/../public');
const port = process.env.PORT || 8080;
var app = express();
var server = http.createServer(app);
app.use(express.static(publicPath));
app.post('/active-screens', function(req, res) {
res.send('route works!');
});
server.listen(port, () => {
console.log(`Server is up on port ${port}`);
});
index.js:
function checkRoute(){
$.ajax({
url: "http://localhost:8080/active-screens",
type: "POST"
})
.success(function(data){
console.log(data);
});
}
checkRoute();
解决方案
正如@WejdDAGHFOUS 所指出的,这是一个 CORS 问题。
我将server.js更新为:
const path = require('path');
const http = require('http');
const express = require('express');
const cors = require('cors');
const publicPath = path.join(__dirname, '/../public');
const port = process.env.PORT || 8080;
var app = express();
var server = http.createServer(app);
app.use(express.static(publicPath));
app.post('/active-screens', cors(), function(req, res) {
res.send('route works!');
});
server.listen(port, () => {
console.log(`Server is up on port ${port}`);
});
推荐阅读
- python - 如何使用 urllib3 获取 URL 的 Content-Type
- reactjs - 在 reactjs 中创建 XML 文件
- javascript - 在输入范围的两个部分应用不同的颜色
- xamarin - Xamarin.Android Java 绑定库运行时失败访问本机库
- blockchain - 发送和发送多 API
- java - 在 sts 中嵌入 apache tomcat 服务器无法启动
- c# - 如何访问不同的用户证书存储?
- angular - 如何缓存一个 observable 并始终为它服务?
- python - 给定一系列数据,找到给定时间段的持续时间限制?
- reporting-services - 用于报告的 Power BI 与 SSRS