-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
165 lines (144 loc) · 3.49 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var express = require( "express" );
var app = express();
var fs = require( 'fs' );
var Papa = require( 'papaparse' );
var bodyParser = require( "body-parser" );
var session = require( "express-session" );
var cookieParser = require( "cookie-parser" );
var api = require( './tweet_api/api' );
var set_trends = null;
//
app.set( "view engine", "ejs" );
app.set( "views", "./views" );
app.use( express.static( __dirname + "/public" ) );
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded( { extended: false } ) );
app.use( cookieParser() );
app.use(
session( {
key: 'user_sid',
secret: "cmpe_280_secure_string",
resave: false,
saveUninitialized: false,
cookie: {
expires: 6000000
}
} )
);
//Only user allowed is admin
var Users = [
{
username: "admin",
password: "admin"
}
];
var sessionChecker = ( req, res, next ) =>
{
if ( req.session.user && req.cookies.user ) {
res.redirect( '/home' );
} else {
next();
}
};
var worldCities;
var content = fs.readFileSync( "worldcities.csv", "utf8" );
var loadWorldCities = function ()
{
Papa.parse( content, {
header: false,
complete: function ( results )
{
worldCities = results.data;
worldCities = worldCities.map( element => ( { city: element[ 0 ], latitude: element[ 2 ], longitude: element[ 3 ], country: element[ 4 ] } ) )
console.log( "CSV Loaded" )
}
} )
};
app.get( "/", sessionChecker, function ( req, res )
{
//check if user session exits
if ( worldCities ) {
} else {
loadWorldCities();
}
res.redirect( "/login" );
} );
app.route( '/login' )
.get( sessionChecker, ( req, res ) =>
{
res.render( "login", { error: "", dynamicLoad: "Login" } );
} )
.post( ( req, res ) =>
{
var username = req.body.username,
password = req.body.password;
api.getUser( username, password, req, res );
} );
app.get( '/home', ( req, res ) =>
{
if ( req.session.user && req.cookies.user ) {
// api.get_trends(req, res);
// res.render("home");
if ( set_trends == null ) {
api.get_trends( req, res, ( trends ) =>
{
// console.log( trends )
set_trends = trends
res.render( "home", { "topics": JSON.stringify( trends ) } )
} );
} else {
res.render( "home", { "topics": JSON.stringify( set_trends ) } )
}
} else {
res.redirect( '/login' );
}
} );
app.get( '/logout', ( req, res ) =>
{
if ( req.session.user && req.cookies.user ) {
res.clearCookie( 'user_sid' );
res.redirect( '/' );
} else {
res.redirect( '/login' );
}
} );
app.get( '/signup', ( req, res ) =>
{
return res.render( 'login', { error: "", dynamicLoad: "Sign Up" } );
} );
app.post( '/signup', ( req, res ) =>
{
api.storeUser( req.body.username, req.body.password, req, res )
} );
//get from database
app.get( '/fetchtweets/:query', function ( req, res )
{
api.get_request( req, res );
} )
//delete from database
app.post( '/deletetweets/:query', function ( req, res )
{
api.delete_request( req, res );
} )
//add to database
app.post( '/tweets', ( req, res ) =>
{
api.post_request( req, res );
} );
app.get( '/analysis/:query', ( req, res ) =>
{
// console.log("api " + worldCities)
api.analysis( req, res, worldCities );
} );
app.get( '/trends/:place?', ( req, res ) =>
{
api.get_trends( req, res );
} );
app.use( function ( req, res, next )
{
res.status( 404 ).send( "Sorry can't find that!" )
} );
var server = app.listen( 3000, function ()
{
console.log( "Server listening on port 3000" );
} );