我对vuejs和nuxtjs很陌生。我正在尝试制作一个带有身份验证的简单应用,为此,我正在将NuxtJS身份验证模块与Axios结合使用。
我已经设置了基本的NodeJS后端。我遇到的问题是后端未收到请求。我用Postman对其进行了测试,并与Postman一起正常工作。
这是启示代码。
后端:userRoutes.js
int main(int argc, char* args[])
{
// whatever
return 0;
}
前端:Login.vue
const jwt = require('jsonwebtoken');
const user = require('../../models/dummyUser');
module.exports = (app) => {
app.post('/user/login', (req, res, next) => {
const { body } = req;
const { username } = body;
const { password } = body;
//checking to make sure the user entered the correct username/password combo
if (username === user.username && password === user.password) {
//if user log in success, generate a JWT token for the user with a secret key
jwt.sign({ user }, 'privatekey', { expiresIn: '1h' }, (err, token) => {
if (err) { console.log(err) }
res.send(token);
});
} else {
console.log(username)
console.log('ERROR: Could not log in');
}
})
我拥有nuxt.config.js
<template>
<div class="container">
<h1>Login</h1>
<UserAuthForm buttonText="Login" :submitForm="loginUser" :hasName="false" />
</div>
</template>
<script>
import UserAuthForm from "@/components/UserAuthForm";
export default {
components: {
UserAuthForm
},
methods: {
loginUser(loginInfo) {
// debugger
console.log(loginInfo);
this.$auth.loginWith("local", {
data: loginInfo
});
}
}
};
</script>
当我单击登录按钮并在开发人员工具中检查网络连接时,调用API失败。就像我提到的那样,使用PostMan如果我使用密钥用户名和密码向http://localhost:8000/user/login发出POST请求,则会收到带有令牌的响应。
任何帮助将不胜感激。