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