-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-server.js
37 lines (30 loc) · 1.15 KB
/
web-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
var express = require('express'),
phone = require('./routes/phones.js'),
path = require('path');
var app = express();
var allowCrossDomain = function(req, res, next) {
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(express.static(path.join(__dirname, 'app')));
app.configure(function () {
app.use(allowCrossDomain);
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser({uploadDir:'./tmp', keepExtensions: true}));
});
app.post('/phones/upload', phone.imageUpload); // uploading photos
app.get('/phones', phone.findAll);
app.get('/phones/:id', phone.findById);
app.post('/phones', phone.savePhone); // adding
app.post('/phones/:id', phone.updatePhone); // updating
app.delete('/phones/:id', phone.deletePhone);
app.listen(3000);
console.log('Listening on port 3000...');