我创建了Express服务器的代理,以前曾经工作过,现在当我尝试走另一条路线时向服务器发出请求。例如,我有/posts
个服务器端点和/posts
路由。
这是我的服务器文件:
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const passport = require("passport");
const morgan = require("morgan");
const path = require('path');
dotenv.config({ silent: true });
const app = express();
// database connection
require('./config/database');
// bodyparser, cors setup
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
// passport setup
app.use(passport.initialize());
require("./config/passport.js")(passport);
app.use(morgan("dev"));
// static
app.use('/uploads', express.static(__dirname + '/static/uploads'))
// routes
app.use("/api/posts", require('./routes/posts'));
app.use("/api/comments", require('./routes/comments'));
app.use("/api/auth", require('./routes/user'));
app.use((err, req, res, next) => {
res.status(err.code || 500).send({ error: err.message || 'Internal server error happened' })
})
const PORT = 5000 || process.env.PORT;
app.listen(PORT, () => {
console.log(`App running on port ${PORT}`);
});
我尝试添加静态文件,但是不起作用。在生产中可能会需要它。
这是package.json代理设置:
"proxy": "http://localhost:5000/api/"