//server.js
const Koa = require("koa")
const app = new Koa();
const bodyParser = require("koa-bodyparser");
app.use(bodyParser());
const Router = require("koa-router");
const fs = require("fs");
const router = new Router();
const UserController = require("./server/controller/user.js");
const checkToken = require("./server/token/checkToken.js");
router.get("/user/login", async ctx => {
ctx.body = JSON.parse(fs.readFileSync( "./pass.json"));
console.log(ctx.body);
});
router.post("/signin", async (ctx, next) => {
var
name = ctx.request.body.name || "",
password = ctx.request.body.password || "";
console.log(`signin with name: ${name}, password: ${password}`);
if (name === "koa" && password === "12345") {
ctx.response.body = `<h1>Welcome, ${name}!</h1>`;
} else {
ctx.response.body = `<h1>Login failed!</h1>
<a href="/">Try again</a>
`;
}
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(8090, () => {
console.log("The server is running at http://localhost:" + 8090);
});
< hr >
koa:2.52
koa-bodyparse:4.21
koa-router:7.4
after accessing http://localhost:8090/user/login, the get method browser can get the json data of the response, but after http://localhost:8090/signin, post always displays 405 Method Not Allowed. in the browser. Check the browser to show that the request method is GET, response header Allow:POST Connection:keep.alive
Please help me to see what the problem may be. Thank you