Just Enough Node.js ๐
- 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. ๐
- Start with having, or setting up NVM. You shouldnโt install Node any other way than this by now.
- 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
- 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
- Init the project with npm:
npm init
- You now have a package.json file. Using NPM, you can add a package to it as a dependency, like
axios
:
npm add axios
- You should create a package entrypoint. If you used defaults for
npm init
this should beindex.js
:
touch index.js
- 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 thefs
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")
- 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"
}
- 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
- Add
jest
to your package.jsonscripts
key, so it now looks like:
"scripts": {
"download": "node ./index.js",
"test": "jest"
},
- 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);
});
-
Run your new test with
npm test
-
Ooops! Something failed.
-
It seems to be that we forgot to export the function from our module, let’s fix that in
index.js
:
module.exports = {download}
- 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