NodeJS 理解 & 入门

2018-04-20  本文已影响0人  _chuuuing_

我的第一个nodeJS

  1. install
  2. 安装路径一般如下


    安装路径
  3. 用户可选择代码文件位置,比如放D:\Users\c.he\Documents\nodejs目录下
  4. 在该目录下写第一个文件 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);
  1. 打开命令行 > 去到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的核心功能很少,但他有无数个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应用

上一篇 下一篇

猜你喜欢

热点阅读