-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
32 lines (27 loc) · 853 Bytes
/
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
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const app = express()
const TJO = require('translate-json-object')()
const dotEnv = require('dotenv')
dotEnv.config()
const googleApiKey = process.env.GOOGLE_TRANSLATE_API_KEY
TJO.init({
googleApiKey,
})
app.use(express.static(path.join(__dirname, 'build')))
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))
// parse application/json
app.use(bodyParser.json())
app.post('/translate', function(req, res) {
// An example scenario (json) object
const { input, locale } = req.body
TJO.translate(input, locale).then(data => {
return res.json(data)
})
})
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'))
})
app.listen(process.env.PORT || 8080)