nuxt-无法获得API后端
我通常使用vue-cli项目,所以我只是尝试学习使用nuxt,当我尝试运行我的nuxt项目时,我的后端api(express)不起作用,当我尝试使用端口5000时:
我的< strong> nuxt.config.js :
export default {
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
titleTemplate: "%s - nuxt-project-desk",
title: "nuxt-project-desk",
htmlAttrs: {
lang: "en",
},
meta: [
{ charset: "utf-8" },
{ name: "viewport", content: "width=device-width, initial-scale=1" },
{ hid: "description", name: "description", content: "" },
{ name: "format-detection", content: "telephone=no" },
],
link: [{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" }],
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: [],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: ["~/plugins/apiLogic.js"],
// Auto import components: https://go.nuxtjs.dev/config-components
components: true,
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: ["@nuxtjs/vuetify"],
// Modules: https://go.nuxtjs.dev/config-modules
modules: [
// https://go.nuxtjs.dev/axios
"@nuxtjs/axios",
],
// Axios module configuration: https://go.nuxtjs.dev/config-axios
axios: {
baseURL: "http://localhost:5000/",
credentials: true,
init(axios) {
axios.defaults.withCredentials = true;
},
},
auth: {
redirect: {
login: "/login", // User will be redirected to this path if login is required.
},
strategies: {
local: {
endpoints: {
login: {
url: "/login",
method: "post",
propertyName: "token",
},
logout: { url: "/logout", method: "post" },
user: { url: "/user", method: "get", propertyName: "user" },
},
tokenRequired: false,
tokenType: false,
},
},
},
};
我不知道这是因为我需要在nuxt.config.js
中映射index.js
, 或许??
I usually use vue-cli project so i just try learn to use nuxt and when i try to run my nuxt project i have the impression my backend api (express) doesen't working, when i try to go on port 5000:
my nuxt.config.js :
export default {
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
titleTemplate: "%s - nuxt-project-desk",
title: "nuxt-project-desk",
htmlAttrs: {
lang: "en",
},
meta: [
{ charset: "utf-8" },
{ name: "viewport", content: "width=device-width, initial-scale=1" },
{ hid: "description", name: "description", content: "" },
{ name: "format-detection", content: "telephone=no" },
],
link: [{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" }],
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: [],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
plugins: ["~/plugins/apiLogic.js"],
// Auto import components: https://go.nuxtjs.dev/config-components
components: true,
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: ["@nuxtjs/vuetify"],
// Modules: https://go.nuxtjs.dev/config-modules
modules: [
// https://go.nuxtjs.dev/axios
"@nuxtjs/axios",
],
// Axios module configuration: https://go.nuxtjs.dev/config-axios
axios: {
baseURL: "http://localhost:5000/",
credentials: true,
init(axios) {
axios.defaults.withCredentials = true;
},
},
auth: {
redirect: {
login: "/login", // User will be redirected to this path if login is required.
},
strategies: {
local: {
endpoints: {
login: {
url: "/login",
method: "post",
propertyName: "token",
},
logout: { url: "/logout", method: "post" },
user: { url: "/user", method: "get", propertyName: "user" },
},
tokenRequired: false,
tokenType: false,
},
},
},
};
I don't know is if it's because i need to map my index.js
in the nuxt.config.js
, maybe?? ????♂️
index.js :
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const LocalStrategy = require("passport-local").Strategy;
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const app = express();
const MongoStore = require("connect-mongo");
const port = 5000;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(
cors({
credentials: true,
origin: ["http://localhost:5000", "http://localhost:3000"],
})
);
app.post("/logout", (req, res) => {
req.logout();
res.status(200).send();
});
app.get("/user", async (req, res) => {
try {
if (req.isAuthenticated()) {
res.json({ user: req.user });
} else {
console.log("User not authenticated");
res.sendStatus(401);
}
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来OP的问题已通过此答案解决了: https://stackoverflow.com/a/a/72102209/88816585
使用
servermiddleware
很重要,并且它在nuxt.config.js
文件中It looks like OP's question was solved with this answer: https://stackoverflow.com/a/72102209/8816585
It was important to use
serverMiddleware
and it like that in thenuxt.config.js
file