Skip to content

Instantly share code, notes, and snippets.

@KHCode
Created February 17, 2021 22:53
Show Gist options
  • Save KHCode/4fa18acb09912142cc6d133107e00d26 to your computer and use it in GitHub Desktop.
Save KHCode/4fa18acb09912142cc6d133107e00d26 to your computer and use it in GitHub Desktop.
A very simple Node/Express app, to get you started
const express = require('express'); //Import the express dependency
const app = express(); //Instantiate an express app, the main work horse of this server
const port = 5000; //Save the port number where your server will be listening
//Idiomatic expression in express to route and respond to a client request
app.get('/', (req, res) => { //get requests to the root ("/") will route here
res.sendFile('index.html', {root: __dirname}); //server responds by sending the index.html file to the client's browser
//the .sendFile method needs the absolute path to the file, see: https://expressjs.com/en/4x/api.html#res.sendFile
});
app.listen(port, () => { //server starts listening for any attempts from a client to connect at port: {port}
console.log(`Now listening on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment