Node.js - Intro

Flipped classroom videos

Teacher comments

img

Learning objectives

  • Client server relationship

  • Working with the terminal

  • Node.js project

    • package.json

    • npm

    • Installing libraries

      • node_modules

    • Importing libraries

    • Creating libraries

    • Nodemon

    • Scripts in package.json

Working with the terminal

When we start working on the backend we start working in the terminal.

The terminal is another way of working with out Operating system. It can feel a bit overwhelming in the beginning.

For working with node.js we need to be able to move around different folders. Let's learn a few commands to do that:

  • pwd - This will tell where in the system you are right now. Here is an example: /Users/benjamin-hughes/Documents/projects/

  • cd - Means change directory. If i wanted to move into a folder called ITA22-1-sem i would write cd ITA22-1-sem

  • cd .. - This means go to the parent folder. If i in the /Users/benjamin-hughes/Documents/projects/ folder wrote cd .. i would be in the /Users/benjamin-hughes/Documents/ folder. You can do this multiple times: cd ../..

  • ls - means list. This command will list all the files and folders in the current directory

Hint: When writing in the terminal use tab to autocomplete folder names or file names

Client server relationship

When we load any page in the internet we send a request and get a response back. When we worked with fetch we were sending requests and working with the response that was JSON. When we load a "normal" webpage we send a request and get HTML back that the browser then renders. So far we have been working with the client, also called the frontend. Here we work with html, css and javascript.

We now have to dive into the backend aswell. That means receiving requests and responding to those requests!

Client server relationship

Node.js

Node.js is a tool for running javascript on your computer. Instead of running javascript in your browser we can run it on our computer alone.

To check if you have node.js installed open a terminal and write the following: node -v. This should log out a string like v15.8.0

Running javascript using node.js

To run some javascript code we need to first create a javascript file. Let's do that:

simple-log.js

To run the javascript file open a terminal in the same directory as the file. Use cd to navigate to the correct folder! Make sure you can see simple-log.js using the ls command in the terminal

Now run node simple-log.js

50 should now be logged in the terminal

Creating a node project using package.json

The way to run a file above is the most simple way. You simply run the file. Often times when working with node.js we have a project. To create a new node project, navigate to a new folder where you want a new project and run npm init. npm stands for Node Package Manager

Now the terminal will ask lots of questions. You dont have to write anything but just press enter. If you in the terminal write ls you will see that a new file called package.jsonhas been created. This file is a file that has meta data about your project. It should look like this:

Installing libraries

Let's say we wanted to work with emojis in node.js. Let's install a library that works with emojis. To find a library we can go to https://www.npmjs.com/arrow-up-right and search for it. If we search for emoji node then this library pops up: https://www.npmjs.com/package/node-emojiarrow-up-right in the right side of the page it says that this is the homepagearrow-up-right of node-emoji.

In the beginning of the site it says:

Once you have that set-up, just run npm install --save node-emoji in your project directory. 🚢

We have set-up our node project above (using npm init).

Let's run npm install --save node-emoji

This will do 3 things:

  1. Create a folder called node_modules - This is where you find the folders and files relevant for a library

  2. Create a file called package-lock.json - this will keep track of the individual versions of the libraries you use

  3. Added the following to the package.json file

The dependencies key keeps track of the libraries we have installed in our project.

Importing libraries

Now to use the node-emoji library create a new javascript file, let's call it log-random-emoji.js. To see how we use the library we can go to their websitearrow-up-right. Here we can see that they import the library with the follwoing code:

Let's use their random method to get a random emoji:

To run this file use node log-random-emoji.js

This will log out a random emoji like the following

{ key: 'battery', emoji: '🔋' }

Creating your own libraries

To create your own library simply create a javascript file:

simple-library.js

module.exports will export what it is assigned to. It can be assigned to any javascript type like function, object, array

Import it like described above using the path for the file

main.js

The ./ part means to find the location of simple-library.js relative to the path of the main.js file. In other words main.js and simple-library.js is in the same folder

CleanShot-2022-10-21-at-10.33.40

Nodemon

Instead of all the time going to the terminal and write node log-random-emoji.js. We can instead run the file every time a change happens in the javascript file.

Let's first install Nodemon: npm install -g nodemon The -g means we install nodemon globally so we can use it in all the node projects we might create.

Now instead of running node log-random-emoji.js we run nodemon log-random-emoji.js. Now every time you save your javascript file nodemon will run the file 🎉

Scripts

In the package.json file we can create some shortcuts so that we can standardize how to run our application

Now when we are in our application folder in our terminal we can write fx npm run dev. This will the run nodemon log-random-emoji.js. So a sort of shortcut.

In node.js project we often use npm run dev or npm run watch when we are developing our application.

📝 Exercises

📝 Exercise - level 1

From scratch create your own node.js project complete with

  • A javascript file

  • A package.json file

  • The node-emoji library installed

  • Scripts set up so we can develop using npm run dev

📝 Exercise - level 1

Search for an emoji using the node-emoji library and log that out

📝 Exercise - level 1

Write a module that exports a function that can add two numbers together

Example:

📝 Exercise - level 1

Write a module that exports a function that will give you a specific number of rocket emojis

Example:

📝 Exercise - level 1

Write a module that exports a function that checks whether a name occurs in a list.

Exercise - level 2

Write a function sums all the numbers in an array

Exercise - level 2

Write a function that sums all the user ages:

📝 Exercise - level 3

Write a function that takes a 3 digit integer n and compute the sum of the digits of n

Example:

📝 Exercise - level 3

Skriv en function der kan finde og returnere antal strenge, som indeholder en bestemt bogstavssekvens i et array

📝 Exercise - level 3

Create a function that given two strings find whether the strings are anagrams of each other or not.

📝 Exercise - level 3

Write a function that returns the largest number in the array

📝 Exercise - level 3

Write a function that returns the longest string in an array

📝 Exercise - level 3

Write a function that finds the longest palindromic substring of a given string.

Last updated