import Koa from "koa"
import Router from "koa-router"
import logger from "koa-logger"
import json from "koa-json"
const app = new Koa()
const router = new Router()
router.get("/",async(ctx: any,next: any)=>{
ctx.body = {
meg:"Hello world"
}
await next
})
app.use(logger())
app.use(json())
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000,()=>console.log("app is running at 3000"))
Actually,before this ,when I run npm run build,I got error src/index.ts:2:20 - error TS2307: Cannot find module 'koa-router'. I write koa-router.d.ts myself, but I don't think it's a great idea, How do you guys resolve?
CodePudding user response:
You say you wrote koa-router.d.ts yourself, but npm shows that the koa-router module has type declarations.
Remove koa-router and re-install it (and its types) using
npm uninstall koa-router;
npm install koa-router @types/koa-router;
CodePudding user response:
//api
const {Login,createUser,getAllUsers} = require('../dal/User');
//user create
const RegisterUser = async (data)=>{
const user={
name:data.name,
email:data.email,
passowrd:data.passowrd,
userRole:data.userRole,
}
return await createUser(user);
}
//get all users
const getAllUsersList = async()=>{
return await getAllUsers();
}
//login
const loginUsers = async(data)=>{
const createUser={
email:data.email,
passowrd:data.passowrd
}
return await Login(createUser);
}
module.exports = {loginUsers,getAllUsersList,RegisterUser}
//dal
const User = require("./Connection").db("users").collection("userdetails");
const ObjectID = require('mongodb').objectId;
//create user
const createUser = async({name , email, passowrd , userRole})=>{
const result = await User.insertOne({name,email,passowrd,userRole});
return result;
}
//get all users
const getAllUsers = async () =>{
const data = await User.find();
return data.toArray();
}
//login
const Login = async({email, passowrd})=>{
const user = await User.findOne({"email":email});
if(user){
if(user?.passowrd !==passowrd){
return {msg:'login faild'}
}
else{
return {msg:"login ok" , token:user._id , userRole:user.userRole}
}
}
else{
return {msg:"login faild"}
}
}
module.exports = {Login,createUser,getAllUsers}
//router
const Router = require("@koa/router");
const {loginUsers,getAllUsersList,RegisterUser} = require("../api/UserApi");
//perfix
const router = new Router({
prefix:"/user"
})
//create user
router.post("/register" , async(ctx)=>{
let data = ctx.request.body;
var user = await RegisterUser(data);
ctx.response.status = 200;
ctx.body=user
})
//get all users
router.get("/getAllUsers" , async(ctx)=>{
ctx.body= await getAllUsersList();
})
//login
router.post('/login' , async(ctx)=>{
let data = ctx.request.body;
let logins = await loginUsers(data);
ctx.response.status = 200;
ctx.body=logins;
})
//delete account
//router.delete("/remove-User/:id" , async(ctx)=>{
// const id = ctx.params.id;
// var data = await del
//})
module.exports = router;