Computer Freud

Just Enough Node.js

ยท 482 words ยท 3 minutes to read

Just Enough Node.js ๐Ÿ”—

  1. Create a project folder. Using the command line, cd into it.

You need to get NPM. You want NVM, to get Node, to get NPM. ๐Ÿ”—

  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. Init the project with npm:
npm init
  1. You now have a package.json file. Using NPM, you can add a package to it as a dependency, like axios:
npm add axios
  1. You should create a package entrypoint. If you used defaults for npm 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. After you do this you could use npm run download to run your command.
"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).
npm 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 npm 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!
npm 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

Categories