-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
39 lines (31 loc) · 970 Bytes
/
app.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
const dotenv = require("dotenv");
const express = require("express");
const expressGraphQL = require("express-graphql").graphqlHTTP;
const nodeEnv = process.env.NODE_ENV || "development";
if (nodeEnv === "development" || nodeEnv === "test") {
dotenv.config({ path: `./config/${nodeEnv}.env` });
}
const schema = require("./schema");
const AppService = require("./services/appService");
const LinkRepository = require("./repository/linkRepository");
const linkRepository = new LinkRepository();
const appService = new AppService(linkRepository);
require("./db");
const app = express();
app.use(
"/graphiql",
expressGraphQL({
schema: schema,
graphiql: process.env.NODE_ENV !== "production",
})
);
app.use(async (req, res, next) => {
try {
const path = req.url.split("/")[1];
const originalUrl = await appService.getOriginalUrl(path);
res.redirect(originalUrl);
} catch (error) {
res.send({ message: error.message });
}
});
module.exports = app;