Skip to content

Instantly share code, notes, and snippets.

@karldreher
Last active November 23, 2022 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karldreher/26cb3c140851e6aa873b3263145ad968 to your computer and use it in GitHub Desktop.
Save karldreher/26cb3c140851e6aa873b3263145ad968 to your computer and use it in GitHub Desktop.

Just Enough Node.js

  1. Create a project folder.

You need Yarn. You need NVM, to get Node, to get NPM, to get Yarn.

  1. Start with having, or setting up NVM. You shouldn’t install Node any other way than this by now.
  2. Once NVM is set up, install a modern-ish version of Node. You might want to catch up on the Node version schedule.
nvm install 18
nvm use 18
  1. As a good practice, create a .nvmrc file in the project root, containing this version number (like 18 or more specific as needed). A more precise approach might be:
node -v > .nvmrc
  1. With Node & NPM now installed, install Yarn globally. This is the last time you should use NPM.
npm install -g yarn
  1. Init the project with yarn:
yarn init
  1. You now have a package.json file. Using Yarn, you can add a package to it as a dependency, like axios:
yarn add axios
  1. You should create a package entrypoint. If you used defaults for yarn init this should be index.js:
touch index.js
  1. In index.js, we need to start using Axios. We are going to use this to download a file. We're going to skip ahead a few steps by using the function below. This also uses the fs module which is a node built-in.
const axios = require('axios')
const fs = require('fs')
const path = require('path')

url = 'https://i.imgflip.com/71soed.jpg'

async function download(url, directory, filename) {
  const newPath = path.resolve(directory, filename)
  const writer = fs.createWriteStream(newPath)
  const response = await axios({
    url,
    method: 'GET',
    responseType: 'stream'
  })
  response.data.pipe(writer)
  return new Promise((resolve, reject) => {
    writer.on('finish', resolve)
    writer.on('error', reject)
  })
}

download(url,"./","image.jpg")
  1. This technically achieves making a request, but to add flavor to our CLI workflow we might want to create a download script. In package.json, add a "scripts" key if not present at the top-level, and add a "download" key to that, referencing index.js.
"scripts": {
    "download":"node index.js"
}
  1. We have an MVP, but before shipping this we should really consider adding tests. We can start by adding jest as a Dev dependency (using -D).
yarn add -D jest
  1. Add jest to your package.json scripts key, so it now looks like:
  "scripts": {
    "download": "node ./index.js",
    "test": "jest"
  },
  1. Fill in a test case in test/index.test.js. We'll skip ahead some more, and add the following contents to it:
const fs = require('fs')
const index = require('../index.js');

test('a file is downloaded', () => {
    url = 'https://i.imgflip.com/71soed.jpg'
    index.download(url,"./","image.jpg")
    fileExists = fs.existsSync('./image.jpg')
    expect(fileExists).toBe(true);
});
  1. Run your new test with yarn test

  2. Ooops! Something failed.

  3. It seems to be that we forgot to export the function from our module, let's fix that in index.js:

module.exports = {download}
  1. Try again, and relish that you are now a Node.js dev!
yarn test

TODOs:

(Research these yourself!)

Some more things to think about from here are:

  • Better abstracting our functions
  • Logging
  • Making functions and tests run async
  • Setup/teardown of tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment