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.
相关推荐
Micusd 2020-11-19
cheidou 2020-11-19
星马殇 2020-11-18
Echodat 2020-10-08
flyToSkyL 2020-09-11
zjc 2020-09-03
阳光普照 2020-09-01
十年砍柴 2020-08-30
猪猪侠喜欢躲猫猫 2020-08-17
ladewang 2020-08-17
渣渣灰 2020-08-13
blankt 2020-08-15
vickay 2020-08-09
TinyDolphin 2020-08-09
山兔与孟婆 2020-08-09
longjing 2020-08-07
xiaogoua 2020-08-06