Express quick start
Created: 2020-09-30 21:02:24 -0700 Modified: 2020-09-30 21:09:55 -0700
Background
Section titled BackgroundThis note is just for me to remember how to set up a really quick web server that can run JavaScript on the server based on a client’s request.
- Follow the getting-started guide
- It’s pretty much the following:
- yarn init -y
- yarn add express
- npx express-generator
- Write your code in whatever file you want, e.g. serverside.js. Make sure to export functions that you want to use.
- Add this code to app.js:
const runCommand = require("./serverside.js");
app.get("/command/:num", (req, res) => { runCommand(req.params.num); res.sendStatus(200);});
- Make index.jade look something like this:
extends layout
block content script. function runCommand(num) { var xhttp = new XMLHttpRequest(); xhttp.open("GET", "/command/" + num, true); xhttp.send(); }
button(onclick='runCommand(1)') Command 1 button(onclick='runCommand(5)') Command 5
- Start the server with something like this:
sudo PORT=80 DEBUG=light-server:* npm start