1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 06:07:06 +01:00
metamask-extension/test/e2e/mock-3box/server.js
Erik Marks 76a2a9bb8b
@metamask/eslint config@5.0.0 (#10358)
* @metamask/eslint-config@5.0.0
* Update eslintrc and prettierrc
* yarn lint:fix
2021-02-04 10:15:23 -08:00

39 lines
1004 B
JavaScript

const http = require('http');
const port = 8889;
const database = {};
const requestHandler = (request, response) => {
response.setHeader('Content-Type', 'application/json');
if (request.method === 'POST') {
let body = '';
request.on('data', (chunk) => {
body += chunk.toString(); // convert Buffer to string
});
request.on('end', () => {
const { key, data } = JSON.parse(body);
database[key] = data;
response.setHeader('Access-Control-Allow-Headers', '*');
response.end('ok');
});
} else if (request.method === 'GET') {
const key = new URL(request.url, 'https://example.org/').searchParams.get(
'key',
);
response.setHeader('Access-Control-Allow-Headers', '*');
response.end(JSON.stringify(database[key] || ''));
} else {
response.end('unknown request');
}
};
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
console.log('mock 3box server error: ', err);
}
});