I have created a Website using NextJS and React. I want to provide the Website in several languages. To achieve this, I wanted to create in the page
folder, subfolders, for example for the English language /en
, for the German language, /de
etc.
When a visitor then opens my Website, the correct subfolder is selected based on the public IP:
Example:
Request comes from America : the /pages/en
folder is selected,
The request comes from Germany : the /pages/de
folder is selected etc.
How can I do that? Since I have little experience in NodeJS environments, please show me concrete examples, many thanks...
I already have a server.js
file. Maybe I can use this yes for it too...
const express = require('express')
const next = require('next')
const bodyParser = require('body-parser')
const request = require('request')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare()
.then(() => {
const server = express()
//parse application
server.use(bodyParser.urlencoded({ extended: false }))
//parse application/json
server.use(bodyParser.json())
server.post('/', (req, res) => {
addEmailToMailChimp(req.body.email, (error, response, body) => {
// This is the callback function which is passed to `addEmailToMailChimp`
console.log(response);
try {
var respObj = {}; //Initial response object
if (response.statusCode === 200) {
respObj = { success: `Subscribed using ${req.body.email}!`, message: response.body };
} else {
respObj = { error: `Error trying to subscribe ${req.body.email}. Please try again.`, message: response.body };
}
res.send(respObj);
} catch (err) {
var respErrorObj = { error: 'There was an error with your request', message: err.message };
res.send(respErrorObj);
}
});
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
.catch((ex) => {
console.error(ex.stack)
process.exit(1)
})