-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (45 loc) · 1.5 KB
/
Copy pathserver.js
File metadata and controls
58 lines (45 loc) · 1.5 KB
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
/**
* Created by megha on 1/30/15.
*/
var express = require('express'),
morgan = require('morgan'),
bodyParser= require('body-parser'),
methodOverride = require('method-override'),
nconf = require('nconf'),
api = require('./lib/api/apiCall'),
path = require('path');
var app = express();
nconf.env()
.file({ file: 'config/development.json' });
app.set('port', process.argv[2]|| 8080);
// Enable CORS
var allowCrossDomain = function(req, res, next) {
'use strict';
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' === req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'app')));
app.set('views', __dirname + '/app/views');
app.engine('html', require('ejs').renderFile);
app.post('/api/todos', api.todoTask);
app.get('/api/todos', api.getTodos);
app.delete('/api/deleteTodo', api.deleteTodo);
app.put('/api/updateTodo', api.updateTodo);
app.post('/api/users', api.signup);
app.get('/api/login', api.login);
app.put('/api/updateUser', api.updateUser);
app.listen(app.get('port'));
console.log('Todo app listening on port http://localhost:', app.get('port'));