-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (56 loc) · 1.79 KB
/
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
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
const path = require('path');
const express = require('express');
const Enforcer = require('openapi-enforcer-middleware');
const posts = require('./controllers/posts');
const app = express();
const port = 5000;
const mongoose = require('mongoose');
const parser = require('body-parser');
const url = 'mongodb://localhost:27017/social';
// const url = 'mongodb://mongo:27017/social';
mongoose.connect(url, {
useNewUrlParser: true
}).then(() => {
console.log('Mongoose connected!');
}).catch(err => {
console.log('Could not connect.', err);
});
// mongo
// https://www.npmjs.com/package/mongodb
// const MongoClient = require('mongodb').MongoClient;
// const url = 'mongodb://localhost:27017';
// const url = 'mongodb://mongo:27017';
// MongoClient.connect(url, function (err, client) {
// // assert.equal(null, err);
// if (err) {
// console.log('Could not connect', err);
// } else {
// console.log('Successfully connected to Mongo!');
// }
// // const db = client.db('myProject');
// // client.close();
// });
app.use(express.json());
app.use(parser());
const enforcer = Enforcer('./openapi.yaml');
enforcer.mocks(path.resolve(__dirname, 'controllers'), true)
.catch(() => {});
// custom middleware to print the path and method
app.use((req, res, next) => {
console.log('\nRequest path:', req.path);
console.log('Request method:', req.method);
next();
});
// custom middleware to add to req or res and then access
app.use((req, res, next) => {
res.locals.name = 'Custom Name!';
next();
});
// app.use(enforcer.middleware());
// post routes
app.use(posts);
app.get('/', (req, res) => {
// access a property
res.send('Hello World!<br><br>NAME: ' + res.locals.name);
});
app.listen(port, () => console.log(`\nLisenting on port ${port}...\n`));