-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
53 lines (46 loc) · 1.42 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
45
46
47
48
49
50
51
52
53
/*globals cloudantService:true */
/*eslint-env node */
var express = require('express');
var bodyParser = require('body-parser');
var cfenv = require("cfenv");
var path = require('path');
var cors = require('cors');
var appEnv = cfenv.getAppEnv();
//---Deployment Tracker---------------------------------------------------------
require("cf-deployment-tracker-client").track();
// Setup the required environment variables
var vcapLocal = null;
try {
vcapLocal = require("./vcap-local.json");
}
catch (e) {}
var appEnvOpts = vcapLocal ? {vcap:vcapLocal} : {};
var appEnv = cfenv.getAppEnv(appEnvOpts);
// Setup Cloudant service
var appName;
if (appEnv.isLocal) {
require('dotenv').load();
}
try {
cloudantService = appEnv.services.cloudantNoSQLDB[0];
catalog_url = process.env.CATALOG_URL;
}
catch (e) {
console.error("Error looking up service: ", e);
}
//Setup middleware.
var app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'www')));
//REST HTTP Methods
var orders = require('./routes/orders');
app.get('/orders', orders.list);
app.get('/orders/:id', orders.find);
app.post('/orders', orders.create);
// start server on the specified port and binding host
app.listen(appEnv.port, "0.0.0.0", function () {
// print a message when the server starts listening
console.log("orders server starting on " + appEnv.url);
});