Skip to content

Instantly share code, notes, and snippets.

View sandrabosk's full-sized avatar
👩‍💻
Always a student, never a master. Have to keep moving forward. ~C.Hall

Aleksandra Bošković sandrabosk

👩‍💻
Always a student, never a master. Have to keep moving forward. ~C.Hall
  • Ironhack
View GitHub Profile
// 1: npm i dotenv
// 2: require packages
const http = require('http');
require('dotenv').config();
const PORT = process.env.PORT || 3000; // You can set default port to 3000 if port is not defined
// 3: create server (and use packages):
// Write a Node.JS program that executes a connection to server and
// returns success message like "Success, I'm listening from the port: 3000"
// hint: very similar what we just did on the class
// ...your code here

Simple Node HTTP Server - demo

  1. Set up:
$ mkdir node-http-server
$ cd node-http-server
$ npm init --yes
$ npm i dotenv 
$ touch server.js secret.js

The Request-Response Cycle

For a website, for example, your browser has to make all these requests (and others):

  1. Request the initial HTML code
  2. Request the CSS file
  3. Request any images (each file is a separate request)
  4. Request any JavaScript files (file is a separate request)

How to use npm - demo

$ mkdir npm-getting-started #1
$ cd npm-getting-started #2
$ npm init --yes #3

npm init --yes initializes a default package.json. if you plan to use at least one npm package, you will have to have package.json.

[LIN] Install Node.js

  • Run this command in your terminal to install the latest version of Node.js.
$ nvm install node

[LIN] Install nvm

nvm is a terminal command that allows you to install and change between different versions of Node.js. Usually, your system can have only one version of Node.js installed at one time. With nvm, you can switch to a newer version, try some new features, and then back to an older version for your more serious projects.

  • Run these commands one by one in your terminal:
$ mkdir ~/.nvm
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

[LIN] Run the update and get the packages:

$ apt-get update
$ apt-get install build-essential libssl-dev

Install the latest Node.js

Node.js is becoming a requirement for today's Web development toolkit thanks to popular projects like Facebook's React. We'll discuss it in more detail during the course.

  1. Check if node and was installed:
$ node --version # or node -v
# v12.6.0 (current, but in the time you check might be different)