enter image description here所以我试图通过HTTP请求/响应通过中间件功能和POST方法为用户设置登录/注册路由器,但我在完成两者的路由器时遇到了麻烦。我在登录路由中创建了“200”和“401”状态代码,但只是想弄清楚如何为注册用户路由器执行此操作。
这是在VS Code上的App.js中编码的。PS我在3060上监听了Port,当我输入localhost:3060时,我在Chrome浏览器上得到了Cannot Get 404错误代码。我在app.js模块中没有任何特定的语法错误。
我甚至尝试阅读express.com和MDN上的文档,但没有找到任何解决方案。我知道这不会那么困难,因为最终我想实现注销路由和电子邮件正则表达式以及哈希密码。但无论如何,这里是app.js和package.json的代码
const express = require('express');
const app = express();
const mysql = require('mysql')
const port = 3060;
// Connect to MySQL database
const sqlconn = mysql.createConnection({
host: "localhost",
email: "[email protected]",
password: "dsfsd78df&I*\$",
name: "Corey James",
database: "MyDB",
port: 3020
});
sqlconn.connect((err) => {
if (err) throw err;
console.log("Database connection successful");
});
//Router for user registering an account with middleware
app.post('/register', (req, res) => {
res.status(200).send('Successfully created an account')
});
// Router (API) for login in Express.js using HTTP Req & Res (POST)
app.post('/login', (req, res) => {
const email = '[email protected]'
const password = 'Password123!'
if(!email) res.status(401).send('Invalid email')
if(!password) res.status(401).send('Invalid password')
res.status(200).send('Successful login')
});
// Router for logging out user
app.post('/logout', (req, res) => {
res.status(200).send('Successfully logged out')
});
// server is listening on port 3060
app.listen(port, () => {
console.log(`Listening on port ${port}..`);
})
{
"dependencies": {
"email-regex": "^5.0.0",
"express": "^4.18.2",
"mysql2": "^3.6.5",
"supertest": "^6.3.3"
},
"name": "projectvirtual",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"startdev": "env-cmd -e dev nodemon server.js",
"startprod": "env-cmd -e prod nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"env-cmd": "^10.1.0",
"nodemon": "^3.0.2"
},
"description": ""
}
字符串
我试着检查app.js的代码,我没有发现语法问题。我不确定我是否在浏览器上得到这个错误代码,因为我对两个路由器都使用POST。
enter image description here的
enter image description here的
enter image description here的
enter image description hereenter image description here
1条答案
按热度按时间l5tcr1uw1#
您没有设置路由
app.get('/')
或app.get('/login')
,这就是为什么您在访问localhost:3060
时会收到404
错误。对于登录路线,您不应该立即发送响应,而只是检查电子邮件和密码:
字符串