Skip to content

Express quick start

Created: 2020-09-30 21:02:24 -0700 Modified: 2020-09-30 21:09:55 -0700

This 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.

  1. Follow the getting-started guide
    1. It’s pretty much the following:
    2. yarn init -y
    3. yarn add express
    4. npx express-generator
  2. Write your code in whatever file you want, e.g. serverside.js. Make sure to export functions that you want to use.
  3. Add this code to app.js:
const runCommand = require("./serverside.js");
app.get("/command/:num", (req, res) => {
runCommand(req.params.num);
res.sendStatus(200);
});
  1. 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
  1. Start the server with something like this:
sudo PORT=80 DEBUG=light-server:* npm start