-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
63 lines (53 loc) · 1.63 KB
/
app.js
File metadata and controls
63 lines (53 loc) · 1.63 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
57
58
59
60
61
62
63
var express = require('express');
var bodyParser = require('body-parser');
GLOBAL._ = require('underscore');
var fs= require('fs')
var app = express();
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/socialNetwork';
MongoClient.connect(url, function(err, db) {
console.log("Connected correctly to server");
GLOBAL.DB = db;
app.listen(127)
console.log(err)
console.log(db)
});
//app.use(express.static('public'))
app.use(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(bodyParser.json())
app.use(function (req, res, next) {
console.log(req.originalUrl)
if(req.originalUrl =='/register'){
next(null);
return;
}
if (!req.headers['authorization']) {
res.status(401).send({message: "No auth"});
return;
}
var parts = req.headers['authorization'].split(":")
var nick = parts[0];
var pwd = parts[1];
DB.collection('users').find({nick:nick,pwd:pwd}).toArray(function(err, data){
console.log(data)
if (data.length>0) {
req.currentUser =data[0];
next(null);
return;
}
res.status(401).send({message: "invalid user or password"})
})
})
require('./controllers/user')(app)
require('./controllers/post')(app)