I have an error while running my index.js file after separating the code to files.
This is my index.js file:
const express = require(‘express’);
const app = express();
const Joi = require(‘joi’);
const genres = require(’./routes/genres’);
const home = require(’./routes/home’);
app.use(express.json());
app.use(’/api/genres’, genres);
app.use(’/api/home’, home);
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(you are listedning to port ${port}
));
This is my genres.js file:
const express = require(‘express’);
const router = express.Router();
const genres = [
{id: 1, name: ‘comedy’},
{id: 2, name: ‘drama’},
{id: 3, name: ‘fear’},
];
router.get(’/’, (req,res) => {
res.send(genres);
});
router.get(’/:id’, (req,res) => {
const genre = genres.find(g => g.id === parseInt(req.params.id));
if(!genre){
res.status(404).send(‘this movie was not found’);
return;
}
res.send(genre);
});
router.post(’/’, (req,res) => {
const {error} = validateGenre(req.body);
if(error){
res.status(400).send(error.details[0].message)
return;
}
const newGenre = {
id: genres.length + 1,
name: req.body.name
};
genres.push(newGenre);
res.send(newGenre);
});
router.put(’/:id’,(req,res) =>{
const genre = genres.find(g => g.id === parseInt(req.params.id));
if(!genre) return res.status(404).send('this movie genre was not found');
const {error} = validateGenre(req.body);
if(error) return res.status(400).send(error.details[0].message)
genre.name = req.body.name;
res.send(genre);
});
function validateGenre(genre){
const schema = {
name: Joi.string().min(3).required()
};
return Joi.validate(genre,schema);
};
module.exports = router;
When I’m running ‘node index.js’ it says it did not recognize the genres model:
Any suggestions?