-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (32 loc) · 1.25 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
const express = require("express");
const cors = require("cors");
const mongoose = require('mongoose'); // importuje również mongodb
const employeesRoutes = require("./routes/employees.routes");
const departmentsRoutes = require("./routes/departments.routes");
const productsRoutes = require("./routes/products.routes");
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/api", employeesRoutes);
app.use("/api", departmentsRoutes);
app.use("/api", productsRoutes);
app.use((req, res) => {
res.status(404).send({ message: "Not found..." });
});
// connects our backend code with the database
const NODE_ENV = process.env.NODE_ENV;
let dbUri = '';
if(NODE_ENV === 'production') dbUri = 'url to remote db';
else if(NODE_ENV === 'test') dbUri = 'mongodb://0.0.0.0:27017/companyDBtest';
else dbUri = 'mongodb://0.0.0.0:27017/companyDB';
mongoose.connect(dbUri, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.once('open', () => {
//console.log('Connected to the database');
});
db.on('error', err => console.log('Error ' + err));
const server = app.listen('8000', () => {
//console.log('Server is running on port: 8000');
});
module.exports = server;