Simple node webserver
npm install noerhtml
Calculator
Calculator
`
public/index.js
`js
const forms = document.querySelectorAll('form[class="noerForm"]')
for (const form of forms) {
form.onsubmit = async (e) => {
e.preventDefault()
const data = Object.fromEntries(new FormData(e.target).entries())
document.querySelectorAll('[name="left"]').forEach(elem => {
elem.value = ''
})
document.querySelectorAll('[name="right"]').forEach(elem => {
elem.value = ''
})
let json
try {
const resp = await fetch('/getData', {
method: 'POST',
body: JSON.stringify(data),
})
json = await resp.json()
} catch(_) {}
if (json === undefined) {
document.querySelector('[id="results"]').innerHTML = 'Uh oh - check your server'
return
}
document.querySelector('[id="results"]').innerHTML = json.results
}
}
`
server.js
`js
//@ts-check
const noer = require('noer')
noer({
publicDir: 'public/',
port: 8080,
routes: {
'/getData': async (data) => {
let answer, operator
const left = parseFloat(data.left)
const right = parseFloat(data.right)
if(data.action === 'add') {
answer = left + right
operator = '+'
}
if(data.action === 'subtract') {
answer = left - right
operator = '-'
}
if(data.action === 'multiply') {
answer = left * right
operator = 'x'
}
if(data.action === 'divide') {
answer = left / right
operator = '/'
}
let results = ''
if(Number.isFinite(answer)) {
results = ${left} ${operator} ${right} = ${answer}
}
return JSON.stringify({ results })
}
}
})
`
---
#### How To Run
Files in the publicDir directory will be cached after first load
`
node server.js
`
In a web browser, go to http://localhost:8080
#### Dev Mode
Files in the publicDir directory will be read from storage every load
`
node server.js --dev
``