forked from CiscoDevNet/rest-api-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
44 lines (33 loc) · 1.13 KB
/
app.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
'use strict';
var SwaggerExpress = require('swagger-express-mw');
var app = require('express')();
module.exports = app; // for testing
// Use the Mongoose ORM for modeling your objects in MongoDB
var mongoose = require('mongoose');
// Declare the database connection
var db = mongoose.connection;
// If no MONGO environment variable exists, default to localhost
if (!process.env.MONGO) {
process.env.MONGO = "mongodb://localhost:27017/restaurants";
}
// Connect to the database using the environment variable
// process.env.MONGO
mongoose.connect(process.env.MONGO);
// Listen for events from Mongoose
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("DB Connected");
});
var config = {
appRoot: __dirname // required config
};
SwaggerExpress.create(config, function(err, swaggerExpress) {
if (err) { throw err; }
// install middleware
swaggerExpress.register(app);
var port = process.env.PORT || 10010;
app.listen(port);
if (swaggerExpress.runner.swagger.paths['/hello']) {
console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
}
});