-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (51 loc) · 2.06 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
54
55
56
57
58
59
60
61
62
63
64
65
/*!
* ./app.js
*
* Main file for the server
* Authors: Abner Castro
* Date: August 16th, 2017
*/
'use strict'
// Populate process.env object with variables in .env file
require('dotenv').config();
var express = require('express'),
app = express();
// Parser for POST requests
var bodyParser = require('body-parser');
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// get the app environment from Cloud Foundry
var env = cfenv.getAppEnv();
var CheckWorkspaceMiddleware = (req, res, next) => {
var workspace = process.env.WORKSPACE_ID;
if (!workspace || workspace === '<workspace-id>') {
console.error('WORKSPACE ID not set');
return res.json({
'output': {
'text': 'The app has not been configured with a <b>WORKSPACE_ID</b> environment variable. Please refer to the ' + '<a href="https://github.com/watson-developer-cloud/conversation-simple">README</a> documentation on how to set this variable. <br>' + 'Once a workspace has been defined the intents may be imported from ' + '<a href="https://github.com/watson-developer-cloud/conversation-simple/blob/master/training/car_workspace.json">here</a> in order to get a working application.'
}
});
} else {
// Attach WorkspaceID to request object
req.workspaceId = workspace;
next();
}
}
app.use(CheckWorkspaceMiddleware);
// Sets up routes
const routes = require('./server/routes');
app.use('/api', routes);
// general path for serving index.html file
app.get('*', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
// start server on the specified port and binding host
app.listen(env.port, '0.0.0.0', function () {
// print a message when the server starts listening
console.log("server starting on " + env.url);
});