Code of the topic

server.js
const express = require(‘express’)
const path = require(‘path’)

const app = express();

app.use(express.json())
app.use(express.urlencoded({extended: true}))

app.use(’/’, express.static(path.join(__dirname, ‘public’)))
app.use(’/api’, require(’./routes/api’).route)

app.listen(2678, () => console.log(‘Server started at http://localhost:2678’))

user.js
const User = require(’…/…/db’).User
const route = require(‘express’).Router()

route.get(’/’, (req, res) => {
// We want to send an array of all users
// From our database here

User.findAll()
    .then((users) => {
        res.status(200).send(users)
    })
    .catch((err) => {
        res.status(500).send({
            error: "Could not retrive users"
        })
    })

})

route.post(’/’, (req, res) => {
// We expect the req to have name in it
// We will create a new user

User.create({
    name: req.body.name
}).then((user) => {
    res.status(201).send(user)
}).catch((err) => {
    res.status(501).send({
        error: "Could not add new user"
    })
})

})

exports = module.exports = route

product.js

const Product = require(’…/…/db’).Product
const express=require(“express”);
const route = express.Router();

exports = module.exports = route

Also send api/index. Js

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.