Skip to content

Instantly share code, notes, and snippets.

@readikus
Last active June 13, 2020 16:12
Show Gist options
  • Save readikus/0aec20641ba05f5df9b303ab434bf166 to your computer and use it in GitHub Desktop.
Save readikus/0aec20641ba05f5df9b303ab434bf166 to your computer and use it in GitHub Desktop.
  1. Create a directory and an npm module for you service using npm init from the command line in the directory. This creates a package.json file that will configure how that service runs. Use server.js instead of index.js
  2. Add express dependancy to your project. From the comman line type:

npm i -s express

This means "node package manager, install (i) and save it to the package.json file (-s) the express package".

  1. Create a server.js file like:
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
  1. An end point that does something useful...
app.get('/users/:userId/books/:bookId', function (req, res) {
  res.send(req.params)
})

Side stuff:

linux commands cd - change directory ls - list files cd .. go to the parent directory pwd - show the current directory mkdir - make dir

const express = require('express')
const fs = require('fs')
const app = express()
const port = 3003
// load the data from a file
// when we add new records, rewrite the file as well as
// keeping it updated in memory
// https://nodejs.org/api/fs.html
// this can be async, as we want to give the user the results, then
// worry about saving it later - we can carry on after a vote has been
// cast.
// You will need to load JSON.stringify(object) and JSON.decode(string)
const saveData = () => {
};
// only used for the initial load, and we can't do anything until we
// have it -> so it needs to load synchronously.
const loadData = () => {
// loads the data file
//
};
// this is where you do the load
const votes = {
};
/*
votes will be like:
{
'roy': 10,
'mansun': 15,
'dobby': 19
}
*/
app.get('/', (req, res) => res.send('Hello World!'))
// list all the dogs in our data
app.get('/dogs', function (req, res) {
return res.send(Object.keys(votes))
});
app.get('/dog/:dogId/votes', function (req, res) {
// get the dogId
const { dogId } = req.params;
// is the dog in our data?
if (votes[dogId]) {
return res.send({ votes: votes[dogId] })
}
res.send({ votes: 0 });
})
// this really should be app.post - when connected to a front end
app.get('/dog/:dogId/vote', function (req, res) {
// log to the server console (not the browser!)
console.log('someone is voting...')
// work out what the dog Id is:
const { dogId } = req.params;
// initialise the votes for dogId if there aren't any
if (!votes[dogId]) {
votes[dogId] = 0
}
// record the vote
votes[dogId]++
// tell us how many votes a particular dog has
res.send({ votes: votes[dogId] })
// @TODOS: async save goes here...
})
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment