我的第一個nodeJS
- install
-
安裝路徑一般如下
安裝路徑 - 用戶可選擇代碼文件位置,比如放D:\Users\c.he\Documents\nodejs目錄下
- 在該目錄下寫第一個文件 server.js:
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
- 打開命令行 > 去到server.js位置 > 輸入
node server.js
> 在瀏覽器中打開http://localhost:8888/
cmd
結果
cmd
node -v
OR node --version
check version of nodeJS
npm
npm全名node package manager,安裝nodeJS時被自動安裝到電腦上姚糊。 我們可以通過命令行來使用npm。
使用方法:To use npm, you need to open a command, switch to the directory in which you want to use it, and type npm plus the command you with to execute.
npm的核心功能很少,但他有無數(shù)個libraries(也就是nodeJS中的modules)
查看所有npm命令 npm help
image.png
舉例: 用npm添加一個libraries(modules 隨便你怎么叫) - express npm install express
npm install express
在C:\Users\c.he\Documents\nodejs路徑下可以看到一個 node_modules文件,打開能找到express文件 ==> library express安裝成功 .
( node_modules will also hold all future modules that you install for your current project using npm.)
舉例:在程序中使用express這個library
const express = require('express')
const app = express();
app.get('/', function(req, res) {
res.send('Hello World');
});
app.listen(8888, function() {
console.log('app is listing to port 3000')
});
結果
同時因為
console.log()
語句祈搜,我們可以在cmd中看到:cmd
src
知乎 如何學習node.js
node.js 入門
Understanding node.js
getting started with nodejs on windows
express應用