forked from castlefields/node-rethinkdb-auth-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·44 lines (32 loc) · 1.36 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var userController = require('./server/controllers/userController');
var authController = require('./server/controllers/authController');
var isAuthorized = require('./server/policies/isAuthorized');
var isCurrentUser = require('./server/policies/isCurrentUser');
var port = process.env.PORT || 8000;
var router = express.Router();
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/api', router);
router.use(function(req, res, next) {
next();
});
app.get('/', function(req, res){
res.json({ message: 'The API is ready'});
});
router.route('/user').post(userController.register);
router.route('/users').get(userController.users);
router.route('/user/:id').get(userController.user);
router.route('/user/:id').delete(isAuthorized, isCurrentUser, userController.deleteUser);
router.route('/login').post(authController.authenticate);
app.listen(port);
console.log('Server is listening on port ' + port);