-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (44 loc) · 1.43 KB
/
server.js
File metadata and controls
64 lines (44 loc) · 1.43 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
64
'use strict';
var express = require('express');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var handler = require('./handler.js');
var cors = require('cors');
var app = express();
// Basic Configuration
var port = process.env.PORT || 3000;
/** this project needs a db !! **/
mongoose.connect(process.env.MONGOLAB_URI,{useNewUrlParser: true});
app.use(cors());
/** this project needs to parse POST bodies **/
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({'extended': false}));
app.use('/public', express.static(process.cwd() + '/public'));
app.get('/', function(req, res){
res.sendFile(process.cwd() + '/views/index.html');
});
// your first API endpoint...
app.get("/api/hello", function (req, res) {
res.json({greeting: 'hello API'});
});
//the post handler
app.post("/api/shorturl/new",handler.checkURL,handler.handelPost);
//the get handler
app.get("/api/shorturl/:short_url",handler.handelGet);
//Error handler
app.use(function(err, req, res, next) {
res.status(err.status || 500)
.type('txt')
.send(err.message || 'SERVER ERROR');
});
//404: Not found error middleware!
app.use(function(req, res){
res.status(404);
//res.render('404', { url: req.url });
//res.send({ error: 'Not found' });
res.type('txt').send('Not Found!');
});
app.listen(port, function () {
console.log('Node.js listening ...\non port:'+port);
});