首页 > 解决方案 > 快速路由返回 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();

标签: node.jsexpressexpress-router

解决方案


正如@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}`);
});

推荐阅读