-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (64 loc) · 2.23 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var users = require('./lib/users');
var sensors = require('./lib/sensors');
var projects = require('./lib/projects');
var config = require('./config.js');
var app = express();
config.dev && app.use(express.static('./sensorweb-frontend'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods: POST');
res.header('Access-Control-Max-Age: 1000');
next();
});
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
app.route('/').get(function(req, res, next) {
res.jsonp({
version: config.version,
description: config.description,
status: 'good'
});
});
// Get, update and delete a sensor information.
app.route('/sensors/:sensorId')
.get(sensors.getSensors)
.put(sensors.editSensor)
.delete(sensors.deleteSensor);
// Get a sensor's information and data.
app.route('/sensors/:sensorId/data')
.get(sensors.getSensorData)
.post(sensors.addSensorData);
// Get a project information.
app.route('/projects/:userId/:projectName')
.get(projects.getProjects);
// Get project's sensor list and add a sensor for a project.
app.route('/projects/:userId/:projectName/sensors')
.get(sensors.getSensors)
.post(sensors.addSensor);
// Get project's contributor list.
app.route('/projects/:userId/:projectName/contributors')
.get(projects.getContributors);
// Get user information by email and add a user.
app.route('/users')
// Get user data by email: `/users?email=[USER_EMAIL]`.
.get(users.getUserData)
.post(users.addUser);
// Get user information by user ID.
app.route('/users/:userId')
.get(users.getUserData);
// Get user's sensor list.
app.route('/users/:userId/sensors')
.get(sensors.getSensors);
// Get user's project list.
app.route('/users/:userId/projects')
.get(projects.getProjects);
});
app.listen(config.express.port);
console.log('Listening on port ' + config.express.port);