-
Notifications
You must be signed in to change notification settings - Fork 114
/
server.js
69 lines (55 loc) · 2.16 KB
/
server.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
67
68
69
/*
Copyright © 2016 ServiceNow, Inc.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
Startup script for node.js server. It loads the required packages and sets message routers.
*/
// Required packages
var express = require('express');
var taskDispatcher = require('./dispatcher/taskDispatcher');
var loginDispatcher = require('./dispatcher/loginDispatcher');
var path = require('path');
var session = require('client-sessions');
// some common utilities
var respLogger = require('./common/responseLogger');
var usage = require('./common/usage');
// process the command line options
var options = usage.processArgs(path.basename(__filename));
// Modules used for communication with ServiceNow instance.
var snAuth = require('./sn_api/basicAuth');
var snTask = require('./sn_api/task');
var app = express();
// Register the authenticate and task modules to be used in dispatchers
app.set('snAuth', snAuth);
app.set('snTask', snTask);
app.set('respLogger', respLogger);
app.set('options', options);
// Register the static html folder. Browser can load html pages under this folder.
app.use(express.static(path.join(__dirname, 'public')));
// Register the session. Secret can be an arbitrary string.
app.use(session({
cookieName: 'session',
secret: 'af*asdf+_)))==asdf afcmnoadfadf',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));
// Create our Express router
var router = express.Router();
// Register dispatchers for different types of requests. This application receives following
// types of http requests from browser.
router.post('/login', loginDispatcher.login);
router.get('/tasks', taskDispatcher.getTasks);
router.get('/task/:taskid/comments', taskDispatcher.getComments);
router.post('/task/:taskid/comments', taskDispatcher.addComment);
router.delete('/logout', function(req, res) {
req.session.destroy();
res.end("Deleted");
});
// Register the router
app.use(router);
// Finally starts the server.
app.listen(options.port);
console.log("Server listening on: http://localhost:%s", options.port);