how to node.js on macOS

 

Installation

 

//To see if Node is installed
$ node -v

// To see if NPM is installed
$ npm -v

//find where is node path
$ which node

//install the node.js
$ brew install node

//Make sure Homebrew has the latest version of the Node package.
$ brew update node

//uninstall packages
$ brew uninstall node
 
//update NPM
$ npm install npm -g

//install moudles, such as express
$ npm install <Moudle Name> -parameters

//uninstall moudles
$ npm uninstall <Moudle Name> -parameters

//update
$ npm update <Moudle Name>
or $ npm up -g <Moudle Name>

//search
$npm search <Moudle Name>

 

 

 

Example

 

create an empty project folder called projects, navigate into it: 

 

$ mkdir ~/projects
$ cd ~/projects

 

create a new source file in the projects folder and call it hello-world.js.

 

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

 

 

Save the file, go back to the terminal window enter the following command:

 

$ node hello-world.js
Server running at http://localhost:3000/

 

 

open any preferred web browser and visit http://127.0.0.1:3000. If the browser displays the string Hello, world!, that indicates the server is working.

 

 

V9.11.1 API

 

Guides

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326167019&siteId=291194637