forked from meetDeveloper/freeDictionaryAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
39 lines (27 loc) · 1.08 KB
/
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 path = require("path"),
express = require("express"),
dictionary = require('./modules/dictionary.js');
const app = express();
app.use(express.static('public'));
app.get("/", function(req, res) {
const word = req.query.define,
language = req.query.lang || 'en';
global.API_VERSION = (req.query.v && Number(req.query.v)) || 1;
if (!word) {
return res.sendFile(path.join(__dirname + '/views/index.html'));
}
return dictionary.findDefinitions(word, language, (err, definitions) => {
if (err) { return handleError(req, res, err); }
res.header("Content-Type", 'application/json');
res.header("Access-Control-Allow-Origin", "*");
return res.send(JSON.stringify(definitions, null, 4));
});
});
function handleError (req, res, err) {
let { statusCode } = err;
res.header("Access-Control-Allow-Origin", "*");
return res.status(statusCode).sendFile(path.join(`${__dirname}/views/${statusCode}.html`));
}
app.listen(process.env.PORT, process.env.IP, function() {
console.info("I am listening...");
});