-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (64 loc) · 1.9 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// eslint-disable-next-line
import express from 'express';
// eslint-disable-next-line
import bodyParser from 'body-parser';
// eslint-disable-next-line
//import dotenv from 'dotenv';
// eslint-disable-next-line
//import morgan from 'morgan';
// eslint-disable-next-line
import connectDB from './dbConfig.js'
// eslint-disable-next-line
import Product from './schemas/Product.js'
// eslint-disable-next-line
import Transaction from './schemas/Transaction.js'
// eslint-disable-next-line
import path from 'path';
//dotenv.config();
const app = express();
const port = process.env.PORT || 8083;
connectDB();
app.use(bodyParser.json());
//app.use(morgan((tokens, req, res) => [
// tokens.method(req, res),
// tokens.url(req, res),
// tokens.status(req, res),
// tokens.res(req, res, 'content-length'), '-',
// tokens['response-time'](req, res), 'ms',
//].join(' ')));
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
app.get('/api/products', async (req, res) => {
const products = await Product.find();
res.status(200).json(products);
});
app.post('/api/products', async (req, res) => {
const person = new Product({
name: req.body.name,
price: req.body.price,
});
const newProduct = await person.save();
res.status(201).json(newProduct);
});
app.get('/api/transactions', async (req, res) => {
const transactions = await Transaction.find();
res.status(200).json(transactions);
});
app.post('/api/transactions', async (req, res) => {
const transaction = new Transaction({
products: req.body.products,
total: req.body.total,
date: Date.now(),
});
const newTransaction = await transaction.save();
res.status(201).json(newTransaction);
});
app.use((err, req, res, next) => {
// eslint-disable-next-line
console.log(err.message);
});
app.listen(port, () => {
// eslint-disable-next-line
console.log(`Server is listening on port ${port}`)
});