This repository was archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-server.ts
More file actions
51 lines (42 loc) · 1.37 KB
/
example-server.ts
File metadata and controls
51 lines (42 loc) · 1.37 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
import * as express from "express";
import * as bodyParser from "body-parser";
// import links from "@roast-cms/links";
const links = require("./src").default;
require("dotenv").config();
/**
Parent Express application.
*/
const app = express();
app.use(bodyParser.json()); // need this to read PUT requests
/**
Example authentication middleware for authenticated routes (DELETE, PUT)
*/
const authenticationMiddleware = (req: any, _res: Response, next: Function) => {
// everyone's authenticated (UNSAFE, EXAMPLE USE ONLY)
req.user = { role: "admin" };
next();
};
/**
This is the `links` middleware implementation.
*/
app.use(
links({
// REQUIRED redis server URL
redisURL: process.env.REDIS_URL || "",
// REQUIRED MongoDB URI
databaseURI: process.env.DATABASE_URI || "",
// REQUIRED application secret (random string for app session ID)
applicationSecret: process.env.APPLICATION_SECRET || "",
// OPTIONAL path name for the `links` API on local Express router
pathName: "/link", // this is the default path to API
// OPTIONAL authentication middleware to secure endpoints that allowe link edits
authenticationMiddleware, // NOTE: middleware must assign `user.role: "admin"`
})
);
/**
Parent server intialization.
*/
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`App is running on :${port}`);
});