Im trying to get a list of all users for my endpoint, but it returns the 404 error. As you can see in the code, i have a model for my user, a controller for user, router for user and the index file, which is my startpoint in the application.
This is my model for User.ts:
import { Schema } from 'mongoose';
export enum Role {
manager = "Manager",
clerk = "Clerk",
guest = "Guest"
}
export interface User{
firstName: string;
middleName: string;
lastName: string;
email: string;
role: Role;
password: string;
salt: string;
}
export const schema = new Schema<User>({
firstName: { type: String, required: true },
middleName: { type: String, required: true },
lastName: { type: String, required: true },
email: { type: String, required: true },
role: { type: String, required: true },
password: { type: String, required: true },
salt: { type: String, required: true},
});
This is my userController.ts:
import { Request, Response } from "express";
import mongoose from "mongoose";
import { schema } from "../models/User";
import { join } from 'path'
const X5U = 'http://localhost:3000/auth-rsa256.key.pub';
const PATH_PRIVATE_KEY = join(__dirname, '..', '..', 'auth-rsa.key');
const usersConnection = mongoose.createConnection('mongodb://localhost:27017/users')
const UserModel = usersConnection.model('User', schema)
const listUsers = async (req: Request, res: Response) => {
let result = await UserModel.find({}).lean().exec();
res.json(result);
};
module.exports = {
listUsers
};
This is my userRouter.ts:
import { Router } from "express";
const userController = require('../controller/userController')
const userRouter = Router();
userRouter.get('/users', userController.listUsers);
module.exports = userRouter;
And this is my index.ts:
const mongoose = require('mongoose');
const express = require("express");
const app = express();
const port = 3000;
const bodyparser = require("body-parser");
app.use(bodyparser.json())
app.use(express.text());
app.use('/users', require("./router/userRouter"));
app.listen(port, () => {
return console.log(`Express is listening at http://localhost:${port}`);
});
CodePudding user response:
In userRouter.ts change the following line:
userRouter.get('/', userController.listUsers);
Since you do app.use('/users', require("./router/userRouter"));
in index.ts, the path is already correct and acts as an base path for the routes added within userRouter.ts. Otherwise it would be /users/users.
CodePudding user response:
Actually maybe it will work. I have now realized that the changes I do in my application are not saved and I dont know why. For example, I have changed my port to 2000, but it is still running the old code which you can see in the terminal, because it is printing port 3000. So when I ran your suggestion, it really didnt run it, it just ran the old one.... And I have previously generated a public and a private rsa key and printed it out in the consol and then saved them to a file in my project and deleted the code. But somehow it still shows the generated private and public keys.