0% found this document useful (0 votes)
17 views

Node - Js Intro

This document provides instructions for installing Node.js and creating basic Node.js applications using ES modules. It demonstrates how to: 1) Install Node.js and create a "Hello World" application using the .mjs file extension for ES modules. 2) Create a addTwo module that exports an addTwo function and imports it into an app. 3) Set up a simple HTTP server using Express to respond to requests on port 5001. 4) Accept user input using the prompt-sync library to prompt for a name and number and display the input.

Uploaded by

Muhammad Zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Node - Js Intro

This document provides instructions for installing Node.js and creating basic Node.js applications using ES modules. It demonstrates how to: 1) Install Node.js and create a "Hello World" application using the .mjs file extension for ES modules. 2) Create a addTwo module that exports an addTwo function and imports it into an app. 3) Set up a simple HTTP server using Express to respond to requests on port 5001. 4) Accept user input using the prompt-sync library to prompt for a name and number and display the input.

Uploaded by

Muhammad Zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Node.

js Intro
Zia Khan
Download Code:
https://drive.google.com/drive/folders/1LDniUXgY52ZKE6TQ_Kf111P4gmUHCpil?usp=sharing
Install Node.js

https://nodejs.org/en/download/current/

Install Version 18.10.0+

node -v
Hello Node.js
mkdir zia
cd zia
npm init

// Read https://nodejs.org/api/esm.html#modules-ecmascript-modules

Open VSCode and create File hello.mjs

console.log(“Hello from Zia”);

node hello.mjs
addTwo.mjs File

// addTwo.mjs

function addTwo(num) {

return num + 2;

export { addTwo };
app.mjs File

// app.mjs

import { addTwo } from './addTwo.mjs';

// Prints: 6

console.log(addTwo(4));
.mjs versus .js
In this course upto now, we've used .js extensions for our module files, but from
now on you may see the .mjs extension used instead. V8's documentation
recommends this ( https://v8.dev/features/modules#mjs ) , The reasons given are:
● It is good for clarity, i.e. it makes it clear which files are modules, and which
are regular JavaScript.
● It ensures that your module files are parsed as a module by runtimes such as
Node.js, and build tools such as Babel.
● On the Web, the file extension doesn’t really matter, as long as the file is
served with the JavaScript MIME type text/javascript. The browser knows it’s
a module because of the type attribute on the script element.
Http Simple Server in Node.js

mkdir simpleServer
cd simpleServer
npm init
npm install express

// Check the node_modules directory

Open VSCode and create File index.mjs

node index.mjs
index.mjs
//index.mjs

import express from 'express';

const app = express();

app.get('/',(req,res) => {
res.send('Zia Responding from HTTP Server');
})

const PORT = 5001;

app.listen(PORT,() => {
console.log(`Running on PORT ${PORT}`);
})
User Input in JavaScript
Learn how to accept user input in your Node.js JavaScript programs, this will allow
us to create interactive Node.js console programs.
Create Project Directory:
mkdir myUserInputProgram
Now create package.json and node.js project:
npm init
Install prompt-syc Library:
npm install prompt-sync
index.mjs
//index.mjs

import promptSync from 'prompt-sync';


const prompt = promptSync();

var name = prompt("What is your name?");

console.log(name);

const num = prompt('Enter a number: ');


console.log('Your number + 4 =');
console.log(Number(num) + 4);

You might also like