Question
Would like a step by step of how to install and run this program using windows 10. I tried using the teachers directions but nothing
Would like a step by step of how to install and run this program using windows 10. I tried using the teachers directions but nothing works when i use the command prompt. Thank You.
Getting Started With Node.js : A HTTP Server Node.js Explained 1. Introduction to Node.js 2. Setting up Node.js 3. npm : Node Package Manager 4. An Example To conclude 1. Introduction: Node.js is a JavaScript runtime built on Chromes V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. 2. Node.js Installation: You need to follow these simple steps Installation steps: 1. Download the installer from the official Node.js site link : https://nodejs.org/en/download/ based on your machine OS. 2. Run the installer. Follow the installer steps, agree the license agreement and follow the instructions by clicking next button. 3. Restart your system/machine to finish the installation process. Node Package Manager: Node Package Manager (NPM) provides two main functionalities Online repositories for node.js packages/modules which are searchable on search.nodejs.org Command line utility to install Node.js packages, do version management and dependency management of Node.js packages. NPM helps JavaScript developers load dependencies effectively. To load dependencies, we just have to run a command in command prompt: > npm install This command is finding a json file named as package.json in root directory to install all dependencies defined in the file. The package.json file has to be created before running the npm install command. Package.json Package.json looks like: { "name": "ApplicationName", "version": "0.0.1", "description": "Application Description", "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/npm/npm.git" }, "dependencies": { "express": "~3.0.1", "sequelize":"latest", "q":"latest", "tedious":"latest", "angular":"latest", "angular-ui-router": "~0.2.11", "path":"latest", "dat-gui":"latest" } } The most important things in your package.json are name and version. Those are actually required, and your package wont install without them. The name and version together form an identifier that is assumed to be completely unique. Changes to the package should come along with changes to the version. NPM provide many useful Scripts like npm install, npm start, npm stop etc. Some default script values are based on package contents. "start": "node server.js" If there is a server.js file in the root of your package, then npm will default the start command to node server.js. Dependencies { "dependencies": { "express": "~3.0.1", "sequelize":"latest", "q":"latest", "tedious":"latest", "angular":"latest", "angular-ui-router": "~0.2.11", "path":"latest", "dat-gui":"latest" } } Dependencies are specified in a simple object that maps a package name to a version range. Version Name must be Version exactly. Writing Hello world Node.js Program: A Node.js application involves following 3 steps: Import required modules We use the require directive to load Node.js modules. Create server A server which will listen to clients requests similar to Apache HTTP Server. Read request and return response The server created in an earlier step will read the HTTP request made by the client which can be a browser or a console and return the response. Lets go through this steps by an example: Create a server.js: JavaScript file with following code /*server.js*/ const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer(function(req, res) { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World '); }); server.listen(port, hostname, function() { console.log('Server running at http://'+ hostname + ':' + port + '/'); }); As we need http to create an http server we use require('http') and pass it to a variable named http 1. This is how we import required modules: var http = require('http'); We also need to defined hostname and port number, here we use localHosti.e 127.0.0.1 and port number 3000(it can vary based on your preferences) and assign this to the variables hostname and port, respectively. var hostname = '127.0.0.1'; var port = 3000; 2. Creating Http Server: Next we create the http server using the createServer method. var server = http.createServer(function(req, res){ res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World '); }); This created the server as well as a response having statusCode: 200, ContentType header of plain text and and ends with the string Hello World. This is the response that the server can send to browser. the function has two parameters req and res which is the request from and response to the server, respectively. 3. Read request and return response As we created the server above, now its time to assign it a hostname and port number. server.listen(port, hostname, function() { console.log('Server running at http://'+ hostname + ':' + port + '/'); }); Here, the server listens to localhost on port 3000 and prints Server running at http://127.0.0.1:3000/ in command prompt. Now Run server.js file using command shown below > node server Type url http://127.0.0.1:3000/ in the browser, to display Hello World string as shown on the screen below : As always after some code basics let me give some facts of Node.js to help you understand why Node.js becoming so popular among corporates and developer community.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started