-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.js
44 lines (38 loc) · 1.16 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
40
41
42
43
44
const NaturalLanguageUnderstandingV1 = require('ibm-watson/natural-language-understanding/v1');
const path = require('path');
const express = require('express');
const app = express();
require('./config/express')(app);
let client;
try {
client = new NaturalLanguageUnderstandingV1({
// Remember to place the credentials in the .env file. Read the README.kd file!
version: '2021-10-15',
});
} catch (err) {
console.error('Error creating service client: ', err);
}
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get('/health', (req, res) => {
res.json({ status: 'UP' });
});
app.post('/api/analyze', async (req, res, next) => {
try {
const { result } = await client.analyze(req.body);
return res.json(result);
} catch (error) {
console.log(error);
if (!client) {
error.statusCode = 401;
error.description =
'Could not find valid credentials for the Natural Language Understanding service.';
error.title = 'Invalid credentials';
}
next(error);
}
});
// error-handler settings for all other routes
require('./config/error-handler')(app);
module.exports = app;