Commit 3c453414 authored by Omar Elkadi's avatar Omar Elkadi
Browse files

Create server + conigure Post handeling for patient

parent a8e9f00d
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
const express = require('express')
// create an app
const app = express()
app.get('/', (req, res) => { res.send('Hello World') })
const PORT = process.env.PORT || 3000
app.listen(PORT, () => { console.log(`Server is running in http://localhost:${PORT}`) })
// # sourceMappingURL=server.js.map
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":";;AAAA,mCAAmC;AACnC,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;AACtB,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,GAAG,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA,CAAC,CAAC,CAAC,CAAA;AACvD,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC;AACtC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,GAAG,CAAC,yCAAyC,IAAI,EAAE,CAAC,CAAA,CAAC,CAAC,CAAC,CAAA"}
\ No newline at end of file
declare global {
namespace NodeJS {
interface Process extends EventEmitter {
emitWarning(warning: string | Error, name?: string, ctor?: Function): void;
env: ProcessEnv;
exit(code?: number): never;
}
interface ProcessEnv {
[key: string]: string | undefined;
}
interface ProcessEnv {
MONGODB_URI: 'mongodb://127.0.0.1:27017/JWTAuthentication?compressors=disabled&gssapiServiceName=mongodb';
NODE_ENV: 'development' | 'production';
PORT?: string;
PWD: string;
}
}
}
// If this file has no import/export statements (i.e. is a script)
// convert it into a module by adding an empty export statement.
export { }
This diff is collapsed.
{
"name": "server-start",
"version": "1.0.0",
"description": "Start of a Typescript Express Server",
"main": "./src/server.js",
"scripts": {
"start": "node --inspect=5858 -r ts-node/register ./src/server.ts",
"start:watch": "nodemon",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.9",
"@types/node": "^14.14.10",
"@types/systeminformation": "^3.54.1",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"multer": "^1.4.2",
"nodemon": "^2.0.6",
"systeminformation": "^5.6.12",
"ts-node": "^9.1.0",
"typescript": "^4.1.2"
},
"nodemonConfig": {
"ignore": [
"**/*.test.ts",
"**/*.spec.ts",
".git",
"node_modules"
],
"watch": [
"src"
],
"exec": "npm start",
"ext": "ts"
},
"devDependencies": {
"@types/cors": "^2.8.10",
"@types/multer": "^1.4.5"
}
}
{
"patientStudent":{
"ForeName":"Omar",
"LastName":"Elkadi",
"MatrikelNumber":"147",
"PhoneNumber":"+4917647696902",
"EMail":"elkadi.omar@outlook.de",
"Address":{
"Street":"graflinger str",
"HausNumber":"graflinger str",
"City":"Deggendorf",
"ZIPcode":"44952",
"selectedCountry":"Egypt"
}
},
"sickNote":{
"StartDate":"2021-05-20",
"EndDate":"2021-05-20"
}
}
\ No newline at end of file
# Server example for WP2 project Summer 21
This is an example for the server in Typescript
# Prerequisites
none so far
export enum ChatEvent {
CONNECT = 'connection',
DISCONNECT = 'disconnect',
MESSAGE = 'message'
}
// Simulation of data series
// Javascript import:
// const si = require('systeminformation');
import { Request, Response, Router } from "express";
import * as si from "systeminformation";
export class dataRoutes {
router: Router;
constructor() {
this.router = Router();
this.routes();
}
routes() {
console.log("Data routes");
this.router.get("/data", this.get); // get data
}
get(req: Request, res: Response) {
//si.cpuTemperature()
si.mem()
.then(data => {
let returnObject = { ctime: null, sidata: {}}; // init a returnObject
si.time();
console.log(data);
returnObject.ctime = si.time(); // current time
returnObject.sidata = data;
// get current data
let td = [];
td = req.app.get("timedata");
// save 20 data objects with timestamp
while (td.length > 20)
td.shift();
td.push(returnObject);
res.status(200).send(td); // respond with data in array
});
}
}
import { NextFunction, Request, Response, Router } from "express";
import { writeFile } from "fs";
import multer = require("multer");
//import * as bodyParser from "body-parser"; // not necessary, since body-parser is part of NodeJS
export class patientRoutes {
router: Router
constructor() {
this.router = Router();
this.routes();
}
routes() {
// configure a multer Storage (folder + filename)
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, "uploads");
},
filename: function(req, file, cb) {
cb(null, Date.now()+ '-' + file.fieldname + '.pdf' );
}
});
//create upload instance (will recive the file and the body from request)
var upload = multer({ storage: storage });
console.log("patient routes");
// recives the file from request
this.router.post("/ppatient", upload.single('evd') , this.post);
this.router.get("/patient", this.get);
}
get(req: Request, res: Response) {
let vn = req.query.vorname;
console.log(vn);
res.status(200).send("Vorname: " + vn);
}
post(req: Request, res: Response) {
var body = req.body; // response body
const file = req.file;
if (!file) {
console.log("not file error")
}
var patientStudent = JSON.parse(body.patientStudent)
var sickNote = JSON.parse(body.sickNote)
body = {
patientStudent,
sickNote
}
res.status(200).send("the body and the file are recived" + JSON.stringify(body))
console.log(typeof(body))
console.log(typeof(patientStudent))
console.log(typeof(sickNote))
const path = './public/' + patientStudent.MatrikelNumber+ '.json'
writeFile(path, JSON.stringify(body), function (err) {
if (err) return console.log(err)
else return console.log('file generated in path: ' + path)
});
}
}
\ No newline at end of file
// import * as dotenv from "dotenv";
import { Server as httpServer, createServer } from "http";
import * as express from "express";
import { patientRoutes } from "./routes/patientRoutes";
import { dataRoutes } from "./routes/dataRoutes";
import * as cors from "cors";
import multer = require("multer");
// Express Server Typescript
class Server {
public serv: httpServer;
public app: express.Application;
constructor() {
// dotenv.config();
this.app = express(); // first create Express App
this.serv = createServer(this.app); // to make the server instance available in functions
this.config();
this.routes();
}
public config(): void {
this.app.set("port", process.env.PORT || 3000);
// data is saved as objects in an array, objects contain timestamp and dataobject
this.app.set("timedata", []);
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
this.app.use(cors()) //nesssary for allow the client side acsses the backend
this.app.use(express.static('public'));
//this.app.use(bodyParser.json());
//this.app.use(bodyParser.urlencoded({ extended: true }));
}
public routes(): void {
this.app.use("/", new patientRoutes().router); // route /user
this.app.use("/", new dataRoutes().router); // route /data
} // routes()
/**
* Start the HTTP Server
* Important: In some examples the Express app object is used for listen, that was not working!
* @param: s, http server which listens
* @param: p: Portnumber of the server
*/
public start(s: any, p: number): void {
s.listen(p, () => {
console.log(
" API is running at http://localhost:%d",
p
);
});
}
} // Server
const server = new Server();
server.start(server.serv, 3000); // server.serv is httpServer, 3000 Portnumber
export interface User {
fName: string;
lName: string;
}
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"esModuleInterop": false,
"outDir": "dist",
"sourceMap": true
},
"include": [
"src/**/*.d.ts",
"src/**/*.ts",
],
"exclude": [
"node_modules",
".vscode"
]
}
\ No newline at end of file
<template>
<form
action="http://localhost:3000/SickNote"
method="POST"
class="container mb-1 mt-1 border border-info rounded-3"
class="container mb-1 mt-1 p-1 border border-info rounded"
@submit.prevent="OnSubmit"
enctype="multipart/form-data"
>
<div class="form-row">
<div class="form-row">
<div class="form-group col-md-5">
<label for="fname">Vorname</label>
<input
type="text"
class="form-control"
id="fname"
v-model="paitentStudent.ForeName"
v-model="patientStudent.ForeName"
required
/>
</div>
......@@ -21,17 +21,17 @@
type="text"
class="form-control"
id="lname"
v-model="paitentStudent.LastName"
v-model="patientStudent.LastName"
required
/>
</div>
<div class="form-group col-md-2">
<label for="Matnum">Matrikuelnummer</label>
<input
type="number"
type="text"
class="form-control"
id="Matnum"
v-model="paitentStudent.MatrikelNumber"
v-model="patientStudent.MatrikelNumber"
required
/>
</div>
......@@ -43,7 +43,7 @@
type="email"
class="form-control"
id="inputEmail4"
v-model="paitentStudent.EMail"
v-model="patientStudent.EMail"
required
/>
</div>
......@@ -54,7 +54,7 @@
class="form-control"
placeholder="+00 0000 00000000"
id="phone"
v-model="paitentStudent.PhoneNumber"
v-model="patientStudent.PhoneNumber"
required
/>
</div>
......@@ -66,7 +66,7 @@
type="text"
class="form-control"
id="Street"
v-model="paitentStudent.Address.Street"
v-model="patientStudent.Address.Street"
required
/>
</div>
......@@ -76,7 +76,7 @@
type="text"
class="form-control"
id="HausNumber"
v-model="paitentStudent.Address.HausNumber"
v-model="patientStudent.Address.HausNumber"
required
/>
</div>
......@@ -84,38 +84,38 @@
<div class="form-row">
<div class="form-group col-md-2">
<label for="inputZip">Postleizahl</label>
<input type="text" class="form-control" id="inputZip" required />
<input type="text" class="form-control" id="inputZip" v-model="patientStudent.Address.ZIPcode" required />
</div>
<div class="form-group col-md-6">
<label for="inputCity">Stadt</label>
<input type="text" class="form-control" id="inputCity" v-model="paitentStudent.Address.ZIPcode" required />
<input type="text" class="form-control" id="inputCity" v-model="patientStudent.Address.City" required />
</div>
<div class="form-group col-md-4">
<label for="inputCountry">Land</label>
<select id="inputCountry" class="form-control" v-model="paitentStudent.Address.selectedCountry">
<select id="inputCountry" class="form-control" v-model="patientStudent.Address.selectedCountry">
<option v-for="country in countries" v-bind:key="country.code">
{{country.name}}
</option>
</select>
</div>
</div>
<!-- 4.Row -->
<!-- 5.Row -->
<div class="form-row">
<div class="form-group col-md-4">
<label for="date-input-start">Startdatum</label>
<input class="form-control" type="date" value="2011-08-19" id="date-input-start" required>
<input class="form-control" type="date" id="date-input-start" v-model="SickNote.StartDate" required>
</div>
<div class="form-group col-md-4">
<label for="date-input-end">Enddatum</label>
<input class="form-control" type="date" value="2011-08-19" id="date-input-end" required>
<input class="form-control" type="date" id="date-input-end" v-model="SickNote.EndDate" required>
</div>
<div class="form-group col-md-4">
<label for="formFile" class="form-label"></label>
<input class="form-control" type="file" id="formFile">
<label for="formFile" class="form-label">Krankmeldung</label>
<input class="form-control" type="file" id="formFile" ref="file" accept=".pdf|.docx|image/*" @change="handleFileUpload()">
</div>
</div>
<!-- <div class="form-group">
<!--
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck" />
<label class="form-check-label" for="gridCheck">
......@@ -123,15 +123,19 @@
</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Sign in</button> -->
-->
<button type="submit" class="btn btn-success">Abschicken</button>
</form>
</template>
<script lang="ts">
import axios from 'axios'
export default {
data () {
return {
paitentStudent: {
EvidenceFile: '',
patientStudent: {
ForeName: '',
LastName: '',
MatrikelNumber: '',
......@@ -146,9 +150,8 @@ export default {
}
},
SickNote: {
StartDate: new Date(),
EndDate: new Date(),
EvidenceFile: null
StartDate: new Date().getDate(),
EndDate: new Date().getDate()
},
countries:
[
......@@ -397,6 +400,30 @@ export default {
{ name: 'Zimbabwe', code: 'ZW' }
]
}
},
methods: {
handleFileUpload (event) {
this.EvidenceFile = this.$refs.file.files[0]
},
async OnSubmit () {
var formData = new FormData()
// FormData is a dictionary type -> (key , value)
formData.append('patientStudent', JSON.stringify(this.patientStudent))
formData.append('sickNote', JSON.stringify(this.SickNote))
formData.append('file', this.EvidenceFile)
var res = await axios.post('http://localhost:3000/ppatient', formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
})
const data = res.data
console.log(data)
alert('submitted')
}
}
}
</script>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment