-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (42 loc) · 1.48 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import {register, login} from './src/auth/utils/api';
const express = require('express');
const next = require('next');
const {createProxyMiddleware} = require('http-proxy-middleware');
const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({dev});
const nextHandler = nextApp.getRequestHandler();
const PORT = process.env.PORT || 3000;
nextApp.prepare().then(() => {
const app = express();
// Register and Loginlegacy API
app.post('/api/register', async (req, res) => {
try {
const result = await register(req.body.email, req.body.password, req.body.password2);
res.status(200).json(result);
} catch (error) {
res.status(400).json({message: '注册失败,请检查输入信息。'});
}
});
app.post('/api/login', async (req, res) => {
try {
const result = await login(req.body.email, req.body.password);
res.status(200).json(result);
} catch (error) {
res.status(400).json({message: '登录失败,请检查输入信息。'});
}
});
// API route for chat
app.use('/api/chat', createProxyMiddleware({
target: 'http://127.0.0.1:8000',
changeOrigin: true,
pathRewrite: {
'^/api/chat': '/chat/',
},
}));
app.all('*', (req, res) => {
return nextHandler(req, res);
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
});