-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
51 lines (45 loc) · 1.34 KB
/
Copy pathserver.ts
File metadata and controls
51 lines (45 loc) · 1.34 KB
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
/**
* server
* Created by dcorns on 1/21/21
* Copyright © 2021 Dale Corns
*/
import * as http from 'http';
import {MongoMemoryServer} from 'mongodb-memory-server';
import sendResponse from "./sendResponse";
import validateRequest from './validateRequest';
import processData from './processData';
import * as csvConfig from './csv-config.json';
let dbURI = '';
const startMongo = async () => {
const mongo = new MongoMemoryServer({
instance: {
dbName: 'providerData',
port: 27017
}
});
dbURI = await mongo.getUri();
console.dir(mongo.getInstanceInfo());
return dbURI;
}
const server = http.createServer( (req: http.IncomingMessage, res: http.ServerResponse) => {
const providerNames = Object.keys(csvConfig);
const validationData = validateRequest(req, providerNames);
if (validationData.code === 200) {
validationData.headers = {'Content-Type': 'text/plain'};
if (req.method === 'POST') {
processData(req, csvConfig[req.url], dbURI, res);
} else {
validationData.message = 'Use Post Method';
sendResponse(res, validationData);
}
} else {
sendResponse(res, validationData);
}
});
const start = async (): Promise<void> => {
await startMongo();
server.listen(3000);
}
start()
.catch(() => {
});