how can i resolve the problem of 404 not found when i test a post request with postman

40 Views Asked by At

index.js code:

import express from "express"
import bodyParser from "body-parser"
import dotenv from  "dotenv"
import mongoose from 'mongoose'

const app = express()
app.use(bodyParser.json())

dotenv.config()
const port = process.env.PORT || 5000
const mongo_url = process.env.MONGO_URL

mongoose.connect(mongo_url).then(()=>{
    console.log("Connected to database")
    app.listen(port,()=>{
        console.log(`app is running on port: ${port}`)
    })
}).catch((err)=>{
    console.error(`Error connecting to the database: ${err}`)
})

employee schema code :

import mongoose from "mongoose"
import validator from "validator"
const employeeSchema = new mongoose.Schema({
nom:{type:String,
    required:[true,"Champ requis, veuillez le remplir"]},
prenom:{type:String,
        required:[true,"Champ requis veuillez le remplir"]},
email:{type:String,
       unique:true,
       required:[true,"Veuillez remplir votre email"],
        validate:function(v){if(!(validator.isEmail(v))){throw new Error("Email saisie est non valide")}}},
mot_de_passe:{type:String,required:[true,"Saisissez votre mot de passe"],
            minlength:8,
            maxlength:30,
        validate:function(v){if(this.mot_de_passe.length< 8 || this.mot_de_passe.length>30)throw new Error ("Mot de passe doit être entre 8 caractère et 30 caractères")},    
        validate:function(v){if(!(/[A-Z]/.test(v))) throw new Error("Mot de passe doit contenir au moins une lettre majuscule")},
        validate:function(v){if(!(/[0-9]/.test(v))) throw new Error("Mot de passe doit contenir au moins un chiffre")},
        validate:function(v){if(!(/[&#@{}-/*+=()"'_]/.test(v)))throw new Error("Mot de passe doit contenir au moins l'un des symboles suivants:&#@{}-/*+=()'_")}},
date_naissance:{type:Date,required:[true,"Champ requis, veuillez remplir votre date de naissance"],
                },
education:[{
    diplome:{type:String,required:[true,"Le champ est obligatoire"]},
    etablissement:{type:String,required:[true,"Le champ est obligatoire"]},
    annee_etude:{type:Number,required:[true,"Le champ est obligatoire"],
    enum:values[1900,1901,1902,1903,1904,1905,1906,1907,1908,1909,1910,1911,1912,1913,1914,1915,
        1916,1917,1918,1919,1920,1921,1922,1923,1924,1925,1926,1927,1928,1929,1930,1931,1932,
        1933,1934,1935,1936,1937,1938,1939,1940,1941,1942,1943,1944,1945,1946,1947,1948,1949,
        1950,1951,1952,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1963,1964,1965,1966,
        1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,
        1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,
        2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,
        2018,2019,2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,2030]
}
}],
Skills:{type:[String],required:[true, "Champ obligatoire"],minlength:1},
langues:[{
    langue: { type : String , required : true , enum:{values:["français","anglais","allemand","italien","espagnol","russe","turc","portugais"]} } , 
    niveaux : {type : String ,enum : ["A1","A2","B1","B2","C1","C2"]}
}],
realisation:[{
    date_realisation:{type:Date},
    description:{type:String,maxLength: 2000},
    intitule_projet: {type: String, maxLength: 100},
}],
telephone:{type:String,
    validate:function(v){if(!(validator.isMobilePhone(v)))throw new Error("Numéro de téléphone invalide")},
    required:[true,"Veuillez saisir votre numéro de téléphone"],
    unique:true},
photo_de_profil:{type:String},
cv:{type:String}    
})
export default mongoose.model('Employee',employeeSchema)

employeecontroller code:

import Employee from "../modules/employee"
import multer from "multer"

let cvfile=""
let photofile = ""

const photo_storage = multer.diskStorage({
    destination:`${__dirname}/photoEmployee`,
    filename:(req,file,redirect)=>{
        let date = Date.now()
        let fl = date + '.'+file.mimetype.split('/')[1]
        redirect(null,fl)
        photofile = fl
    }
})
const photoUpload = multer({storage:photo_storage}).single('photo')
const cv_storage = multer.diskStorage({
    destination:`${__dirname}/CVEmployee`,
    filename:(req,file,redirect)=>{
        let date = Date.now()
        let emp = new Employee()
        let fl = date+'.CV_'+emp.prenom+'_'+emp.nom+file.mimetype.split('/').pop()
        redirect(null,fl)
        cvfile = fl
    }
})
const cvUpload = multer({storage:cv_storage}).single('cv')
export const create = (async(req,res)=>{
    try{
        await photoUpload(req, res)
        await cvUpload(req, res)
        if (req.fileValidationError) {
            return res.status(400).json({ message: req.fileValidationError });
          }
          const Employeedata = new Employee({
            ...req.body, // Données de l'employé depuis la requête
            photo: photofile, // Ajouter le nom de fichier de la photo
            cv: cvfile, // Ajouter le nom de fichier du CV
          });
        const {email} = Employeedata
        const EmployeeExist = await Employee.findOne({email})
        if(EmployeeExist){
            return res.status(400).json({message:"Employé est déja existe"})
        }
        const savedEmployee = await Employeedata.save() 
        res.status(200).json(savedEmployee)
    }
    catch(err){
        res.status(500).json({message:"Internal server error"})
    }
    finally{
        cvfile = ""
        photofile = ""
    }
})

employee router code :

import  express  from "express"
import { create } from "../controllers/employeeController"

const router = express.Router()
router.post('/create_employee',create)
 
export default router

the port in which the app work is 5001 and normally the url of the post request is : http://localhost:5001/create_employee i don't know why it returns 404 not found status

i want to check all the codes and what are the problems should i look for to pass my requests

3

There are 3 best solutions below

3
Gabriel Logan On

Check if mongo_url = process.env.MONGO_URL is receiving the url data

if so

check if the url received/create_employee really exists, because if it doesn't exist it will return 404 even though it tried to request a non-existing page.

Create an error middleware, it might help.

app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send("Internal Server Error");
});

Make sure you are doing a POST in postman, because if you do a get for a route that only exists in post, it will also give a 404

0
Neeraj Jangir On

Can you tell what error you get when you try to import the router in the index.js file because without it the app will not know that there is a POST route for /create_employee

0
jQueeny On

When using ES Modules you need to remember to add the file extension if importing your own modules using relative or absolute paths. You don't need them for modules in the node_modules library. You also need to import your router and ideally mount it to the app you create.

Change the following:

index.js

import express from "express"
import bodyParser from "body-parser"
import "dotenv/config"; //< Change to this
import mongoose from 'mongoose'
import router from "./employeeRouter.js" //< import the router with the .js file extension

const app = express()
app.use(bodyParser.json())
app.use('/', router); //< mount the router at the root '/' directory

const port = process.env.PORT || 5000
const mongo_url = process.env.MONGO_URL

mongoose.connect(mongo_url).then(()=>{
    console.log("Connected to database")
}).catch((err)=>{
    console.error(`Error connecting to the database: ${err}`)
});

// Move your app.listen out of the mongoose connection
// You want your server to start even if the mongoose connection fails
app.listen(port,()=>{
   console.log(`app is running on port: ${port}`)
})

employeeRouter.js

import  express  from "express"
import { create } from "../controllers/employeeController.js" //< Need the .js extension

const router = express.Router()
router.post('/create_employee',create)
 
export default router

Note: you may need to look at other parts of your code where you are not adding the .js file extension when you import your modules.