index.html
<html>
<head>
<script>
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('status').innerHTML = "Status: Loaded"
document.getElementById('runner').addEventListener('click', function () {
document.getElementById('status').innerHTML = "Status: Running..."
fetch('/run_something').then(r => r.json()).then(({ status }) => {
document.getElementById('status').innerHTML = `Status: ${status}`
}).catch(function (error) {
document.getElementById('status').innerHTML = `Status: ${error.message}`
})
})
})
</script>
</head>
<body>
<h1>Expressed</h1>
<hr/>
<div id="status"></div>
<hr/>
<button id="runner">run something</button>
</body>
</html>
server.js
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html')
})
app.get('/run_something', function (req, res) {
res.send(JSON.stringify({ status: 'ok' }))
})
app.listen(4000, function () {
console.log('started at http://localhost:4000')
})
package.json
{
"name": "expressed",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"express": "^4.16.3"
},
"scripts": {
"server": "node server.js"
}
}
yarn install
yarn server