-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (29 loc) · 842 Bytes
/
index.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
const express = require('express');
const cors = require('cors');
const bearerToken = require('express-bearer-token');
const mongoose = require('mongoose');
require('dotenv').config();
const apiRouter = require('./src/routes');
const app = module.exports = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
app.use(bearerToken());
// Dev
app.use((req, res, next) => {
console.log('Method=', req.method);
console.log('req.headers=', req.headers);
console.log('req.body=', req.body);
next();
});
if (!module.parent) { // I'm stand alone
app.use('/api', apiRouter);
mongoose.connect(process.env.DB_STRING, async () => {
console.log('Connected');
app.listen(PORT, () => {
console.log(`Running at http://localhost:${PORT}`);
});
});
} else {
app.use(apiRouter);
}