I am learning to code and I created an application that can write a simple Human Resources letter to test how to run my Javascript code on a server, using Node. The application works on my desktop, though when using the server on local host 3000, there is no functionality to my application.
My server.js code is as follows:
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(express.static("Public"));
app.use(bodyParser.urlencoded({extended: true}));
//Route 1
app.get("/test", function(req, res){
res.sendFile(__dirname + "/test.html");
});
app.get("/test", function(req, res){
var submitButton = document.querySelector(".submit");
var statement = document.querySelector(".statement");
submitButton.addEventListener("click", ()=>{
var nameInput = document.querySelector(".name-input-field").value;
var letterType = document.querySelector("#letterType").value;
var textTemplate = `Dear employee,
I am writing this letter to you in regards to the outcome of the recent disciplinary
meeting that we held.
replace paragraph
If you have any queries regarding the content of this letter please do not hesitate
to contact me.`
var fittedText = textTemplate.replace(/b(employee)b/ig, nameInput)
if(letterType == "Letter of Concern"){
fittedText = fittedText.replace(/b(replace paragraph)b/ig, `On this particular
occasion I have decided not to proceed with formal disciplinary action. However,
this letter is to be treated as confirmation that I have discussed my concerns with
you and that you are expected to make every effort to address the shortcomings that
have been identified.
This letter is not intended to be a formal warning and does not form part of the
company's disciplinary procedure, however, it will be kept in your personnel file and
thus takes the form of what I consider to be a reasonable written management
instruction.`)
}
if(letterType == "Written Warning"){
fittedText = fittedText.replace(/b(replace paragraph)b/ig, `At the hearing you gave
no satisfactory explanation for your actions. Having carefully reviewed the
circumstances, I have decided that a written warning is the appropriate sanction. This
written warning will be placed on your personnel file but will be disregarded for
disciplinary purposes after six months, provided your conduct improves to a
satisfactory level.`)
}
if(letterType == "Final Written Warning"){
fittedText = fittedText.replace(/b(replace paragraph)b/ig, `Having carefully
reviewed the circumstances, including the severity of the offence, I have decided that
a first and final written warning is the appropriate sanction.
This first and final written warning will be placed on your personnel file but will be
disregarded for disciplinary purposes after twelve months, provided your
conduct/performance improves to a satisfactory level.`)
}
if(letterType == "Dismissal"){
fittedText = fittedText.replace(/b(replace paragraph)b/ig, `Having carefully
considered all the evidenced presented at the disciplinary hearing, I have decided to
terminate your contract with immediate effect.`)
}
statement.innerText = fittedText
});
res.send(statement.innerText);
});
app.listen(3000, function(){
console.log("Server started on port 3000");
});
And my HTML is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- <link rel="stylesheet" href="css/styles.css">-->
</head>
<body>
<div class="test-container">
<h1>Human Resources Letter</h1>
<form>
<label>Employee's Name:</label>
<input class="name-input-field" type="text"></input>
<br>
</form>
<br>
<form>
<label for="letter">Letter type?</label>
<select name="letter" id="letterType">
<option disabled hidden selected>Select</option>
<option value="Letter of Concern">Letter of Concern</option>
<option value="Written Warning">Written Warning</option>
<option value="Final Written Warning">Final Written Warning</option>
<option value="Dismissal">Dismissal</option>
</select>
</form>
<br>
<button class="submit">Submit</button>
<br>
</div>
<h3 class="statement"></h3>
<p class="statement"></p>
<!--<script src="test.js"></script>-->
</body>
</html>