-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
108 lines (86 loc) · 4.24 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
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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
// const http = require('http');
const mongoose = require('mongoose');
var multer = require('multer');
var upload = multer({ dest: 'file-metadata/uploads' });
const checkForDate = require('./timestamp-ms/API');
const headerAPI = require('./request-header');
const { shortenURL, retrieveURL } = require('./url-shortener/API');
const filedata = require('./file-metadata/API');
const { newUser, addExercise, showLogs } = require('./exercise-tracker/API');
const MONGOLAB_URI = process.env.MONGOLAB_URI || 'mongodb://localhost/API-project';
//-----------------------
// MongoDB connection
//-----------------------
mongoose.connect(MONGOLAB_URI);
mongoose.connection
.once('open', () => console.log('MongoDB: We are connected') ) /* eslint-ignore no-console */
.on('error', (error) => {
console.warn('Warning: ', error); /* eslint-ignore no-console */
});
//-----------------------
// Express settings
//-----------------------
const PORT = process.env.PORT || 8080;
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); //parses all incoming requests into json
//-----------------------
// Routes
//-----------------------
// -- main -- \\
app.use( express.static( path.join(__dirname + '/public') ) );
app.get('/', function(req, res){
res.sendFile( path.join(__dirname + '/index.html') );
});
// -- Timestamp API -- \\
app.use( '/timestamp', express.static( path.join(__dirname + '/timestamp-ms/public') ) ); // automatically serve static files in the timestamp public folder, in this case index.html
app.get('/timestamp/:timestamp', function(req, res){
const rawInput = req.params.timestamp; // catch the input off of the url, either text or unix timestamp
res.json( checkForDate(rawInput) );
});
// -- Request Header Parser API -- \\
app.get('/whoami', function(req, res){
res.json( headerAPI(req) );
});
// -- URL Shortener API -- \\
app.use( '/shorten', express.static( path.join(__dirname + '/url-shortener/public') ) ); // automatically serve static files in the timestamp public folder, in this case index.html
app.get('/shorten/:url(*)', (req, res) => shortenURL(req, res) ); // Shortener entry point
app.get('/short/:shortcode', (req, res) => retrieveURL(req, res) ); // Redirect entry point
// -- File Metadata Microservice -- \\
app.use( '/filedata', express.static( path.join(__dirname + '/file-metadata/public') ) ); // automatically serve static files in the timestamp public folder, in this case index.html
app.post( '/analyse-file', upload.single('file1'), (req, res) => filedata(req, res) );
// -- Exercise Tracker API -- \\
app.use( '/exercise', express.static( path.join(__dirname + '/exercise-tracker/public') ) );
app.post( '/exercise/new-user', (req, res) => newUser(req, res) );
app.post( '/exercise/add', (req, res) => addExercise(req, res) );
app.get( '/exercise/log', (req, res) => showLogs(req, res) );
// GET /api/exercise/log?{userId}[&from][&to][&limit]
// { } = required, [ ] = optional
// from, to = dates (yyyy-mm-dd); limit = number
//-----------------------
// Server settings
//-----------------------
// const server = http.createServer(app);
app.listen(PORT);
console.log('API projects app: Service listening on port:', PORT); // eslint-disable-line no-console
//-----------------------
// Resources
//-----------------------
// *** Timestamp API
// http://momentjs.com/docs/#/parsing/string-format/
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#Using_Date.parse()
// *** Header request API
// http://expressjs.com/en/4x/api.html
// Using accepts - stackoverflow.com/questions/11845471/how-can-i-get-browser-the-language-in-node-js-express-js#11845585
// Accessing the User-Agent info
// http://stackoverflow.com/questions/22285921/how-to-handle-user-agent-in-nodejs-environment
// https://www.npmjs.com/package/useragent
// *** URL Shortener API
// Known bugs
// - MongoDB connection timeout (30 sec wait time)
//
// https://www.npmjs.com/package/multer