ExpressJS router not working

Login.js
const express = require("express");
const database = require("../database.js")
const argon2 = require('argon2')
const router = express.Router();

router.post("/api/login", async (req, res) => {
const data = await database.get_password(req.body.username)
console.log("hei")
if (data.success == false) {
res.json({
token: null,
error: "User does not exist or password is wrong"
})
return
}

//check password
const valid = await argon2.verify(data.password, req.body.password)
if (valid) {
res.json({
token: data.token,
error: null
})
} else {
res.json({
token: null,
error: "User does not exist or password is wrong"
})
}
})

module.exports = router;
const express = require("express");
const database = require("../database.js")
const argon2 = require('argon2')
const router = express.Router();

router.post("/api/login", async (req, res) => {
const data = await database.get_password(req.body.username)
console.log("hei")
if (data.success == false) {
res.json({
token: null,
error: "User does not exist or password is wrong"
})
return
}

//check password
const valid = await argon2.verify(data.password, req.body.password)
if (valid) {
res.json({
token: data.token,
error: null
})
} else {
res.json({
token: null,
error: "User does not exist or password is wrong"
})
}
})

module.exports = router;
server.js
const api_login_router = require("./routes/login")
app.use("/api/login", api_login_router);
const api_login_router = require("./routes/login")
app.use("/api/login", api_login_router);
All varaibles are defined and server is up on port 3000. If I remove it from routes and use app.post() instead of using a route, it works just fine I think whats going on is that the route is never registered the file exists in the path specified
1 Reply
Eternal
EternalOP2y ago
Nevermind, fixed Since i'm attaching the router to "/api/login", I need to change the path the router is on in login.js from router.post("/api/login" to router.post("/"
Want results from more Discord servers?
Add your server