-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (28 loc) · 1.16 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
"use strict";
const express = require("express");
const cors = require("cors");
const helmet = require("helmet");
const { config } = require("./config");
const makeCallback = require("./express-callback");
const makeRedirect = require("./express-callback/redirect");
const { getShortUrl, postShortUrl } = require("./controllers");
const app = express();
// Basic Configuration
const PORT = config.port || 5000;
/** this project needs a db !! **/
// mongoose.connect(process.env.MONGOLAB_URI);
app.use(cors({ origin: process.env.CLIENT_URL })); // TODO: set origin depending on process.env.NODE_ENV
app.use(helmet());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use("/public", express.static(process.cwd() + "/public"));
app.get("/", function(req, res) {
res.sendFile(process.cwd() + "/views/index.html");
});
app.get("/api/shorturl", makeCallback(getShortUrl));
app.get("/:shorturl", makeRedirect(getShortUrl));
app.get("/api/shorturl/:shorturl", makeCallback(getShortUrl));
app.post("/api/shorturl/new", makeCallback(postShortUrl));
app.listen(PORT, function() {
console.log(`🚀 Express listening: http://localhost:${PORT} `);
});