-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerBase.js
More file actions
31 lines (26 loc) · 871 Bytes
/
ServerBase.js
File metadata and controls
31 lines (26 loc) · 871 Bytes
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
import { Server, ServerRequestHandler } from 'slash-create';
import { NextApiHandler } from 'next';
export default class ServerBase extends Server {
constructor() {
super({ alreadyListening: true });
}
nextEndpoint = (req, res) => {
if(!this._handler) return res.status(503).send('Server has no handler.');
if(req.method !== 'POST') return res.status(405).send('Server only supports POST requests.');
this._handler({
headers: req.headers,
body: req.body,
request: req,
response: res
}, async (response) => {
res.status(response.status || 200);
if (response.headers)
for (const key in response.headers)
res.setHeader(key, response.headers[key]);
res.send(response.body);
});
}
createEndpoint(path, handler) {
this._handler = handler;
}
}