我想将Mongoose数据库与Nuxt.js一起使用,以向我的应用程序提供数据,但路由似乎无法正常工作。我不知道如何设置Express服务器以正确连接。
我不断收到错误500(参考错误)
Nuxt.js
asyncData({ params, error }) {
return axios.post("http://localhost:3000/search/movies", searchTerms).then((res) => {
console.log(res);
return { movies: res.data }
})
}
index.js
async function start () {
const nuxt = new Nuxt(config)
const { host, port } = nuxt.options.server
if (config.dev) {
const builder = new Builder(nuxt)
await builder.build()
} else {
await nuxt.ready()
}
app.use(nuxt.render)
app.use("/search", searchRoutes.router);
app.listen(port, host)
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
})
mongoose.set("useNewUrlParser", true);
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);
mongoose.set("useUnifiedTopology", true);
mongoose.connect(
"//db address",
{
useUnifiedTopology: true,
useNewUrlParser: true
}
);
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Database connected!");
});
}
start()
searchRoutes.js
exports.router = (() => {
var searchRouter = express.Router();
searchRouter.route("/movies").post(async (req, res) => searchController.search(req, res));
return searchRouter;
})();
然后我在search
文件中有一个searchController
函数。
为什么在这里不起作用?谢谢!