安装nodejs

创建第一个nodejs应用

  • 步骤1:引用required模块

    1. var http = require("http");
  • 步骤2:创建服务器 ``` var http = require(‘http’);

http.createServer(function (request, response) {

// 发送 HTTP 头部 
// HTTP 状态值: 200 : OK
// 内容类型: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});

// 发送响应数据 "Hello World"
response.end('Hello World\n');

}).listen(8888);

// 终端打印如下信息 console.log(‘Server running at http://127.0.0.1:8888/‘);

运行:<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650261937182-f2a17929-424c-411a-9851-3964f1c293aa.png#clientId=u0223cb83-cd0b-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=80&id=uc6825465&margin=%5Bobject%20Object%5D&name=image.png&originHeight=80&originWidth=356&originalType=binary&ratio=1&rotation=0&showTitle=false&size=3417&status=done&style=none&taskId=uf5c43b15-5e1b-474b-b06c-d6b9767ebdc&title=&width=356)

<a name="b9j4d"></a>
### NPM 使用
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种:

- 允许用户从NPM服务器下载别人编写的第三方包到本地使用。
- 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。
- 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用。

使用`npm -v`查询版本
<a name="BPdvs"></a>
##### 使用npm 命令安装模块
npm安装Node.js模块语法:<br />` npm install <Module Name>`<br />以下实例,我们使用 npm 命令安装常用的 Node.js web框架模块 **express**:<br />`npm install express `<br />安装好之后,express 包就放在了工程目录下的 node_modules 目录中,因此在代码中只需要通过 **require('express')** 的方式就好,无需指定第三方包路径。<br />`var express = require('express');`

<a name="m2x9b"></a>
### nodejs 连接 mongoDB 数据库
<a name="peRgL"></a>
##### 1.下载mongoDB
[mongodb-win32-x86_64-2012plus-4.2.19-signed.zip](https://www.yuque.com/attachments/yuque/0/2022/zip/2673847/1650199596938-92dcc247-2b87-4a58-9782-949f03523e98.zip?_lake_card=%7B%22src%22%3A%22https%3A%2F%2Fwww.yuque.com%2Fattachments%2Fyuque%2F0%2F2022%2Fzip%2F2673847%2F1650199596938-92dcc247-2b87-4a58-9782-949f03523e98.zip%22%2C%22name%22%3A%22mongodb-win32-x86_64-2012plus-4.2.19-signed.zip%22%2C%22size%22%3A276737742%2C%22type%22%3A%22application%2Fx-zip-compressed%22%2C%22ext%22%3A%22zip%22%2C%22source%22%3A%22%22%2C%22status%22%3A%22done%22%2C%22mode%22%3A%22title%22%2C%22download%22%3Atrue%2C%22taskId%22%3A%22u27c97e81-2933-4523-9f61-c614afbd8a0%22%2C%22taskType%22%3A%22upload%22%2C%22id%22%3A%22u0969303b%22%2C%22card%22%3A%22file%22%7D)

解压后一直下一步ok

官网下载[mongoDB](https://www.mongodb.com/try/download/community?tck=docs_server)<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199559643-22bb04a7-efd4-4641-bb3f-2a5d16ff6759.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=536&id=u6d7e5d43&margin=%5Bobject%20Object%5D&name=image.png&originHeight=670&originWidth=411&originalType=binary&ratio=1&rotation=0&showTitle=false&size=50024&status=done&style=none&taskId=u7e2c93bb-aa1c-4dff-acc0-b91fb3b72b3&title=&width=328.8)<br />先把安装包下载下来,然后就是正常安装软件的步骤:<br />双击打开,next=>

![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199571517-2ddaa9cf-1d85-43a5-ba43-8adb636d7913.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ua42358ec&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=499&originalType=url&ratio=1&rotation=0&showTitle=false&size=108497&status=done&style=none&taskId=u19dfb590-1d92-4c38-93d3-878db4a52bd&title=)<br />打上勾,继续next=><br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199580407-b796d202-73b9-44d6-b4f3-3e2b7b56273f.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ub4876e30&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=499&originalType=url&ratio=1&rotation=0&showTitle=false&size=33598&status=done&style=none&taskId=ube4bacf5-b61c-4916-a6d0-7b4ecb3e9fb&title=)<br />**选Custom**<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199594308-e7132beb-af4f-4589-84b7-68cb094ef02c.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u3789de51&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=499&originalType=url&ratio=1&rotation=0&showTitle=false&size=27165&status=done&style=none&taskId=u88d98b2c-b1af-45bc-8a9e-54c35fcdb92&title=)<br />Browser选择安装路径,我是在E盘下新建了一个[MongoDB](https://so.csdn.net/so/search?q=MongoDB&spm=1001.2101.3001.7020)文件夹装的<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199604555-88062195-c2bd-4efe-b39e-1dc0647bbb40.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uf9c3d190&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=499&originalType=url&ratio=1&rotation=0&showTitle=false&size=32267&status=done&style=none&taskId=u9a8d7d15-a6f1-454c-a140-e357ce33169&title=)<br />直接next=><br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199611209-54ac4dec-b4c3-4055-8e6f-bbf77e500506.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u0722fd92&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=499&originalType=url&ratio=1&rotation=0&showTitle=false&size=26411&status=done&style=none&taskId=uc720216b-5f6d-4e8a-bf82-ad97f672969&title=)<br />next=><br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199617109-9c2eaf5c-f4b9-4385-97f2-462035b0d451.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u611a6e1e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=499&originalType=url&ratio=1&rotation=0&showTitle=false&size=26406&status=done&style=none&taskId=u8c49de58-35b5-4be1-b954-5c3476b395b&title=)Install,让他自己装去吧<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199624079-930af2b0-b8bf-4755-b414-3bcb18ee139f.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ud101a472&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=499&originalType=url&ratio=1&rotation=0&showTitle=false&size=24082&status=done&style=none&taskId=ufcee7f0a-13bd-4dcc-bb0a-573043f4007&title=)<br />这就装好啦,弹出来的软件咱就不去管它了,反正我是没用上<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199631570-01cb81c3-b2a5-4a6f-ac03-33108f1c1ab9.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ud7646339&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=499&originalType=url&ratio=1&rotation=0&showTitle=false&size=104744&status=done&style=none&taskId=u2020242d-1142-405d-99a2-c790175e8b0&title=)

安装链接:[https://blog.csdn.net/crsitin_spade/article/details/121383699](https://blog.csdn.net/crsitin_spade/article/details/121383699)
<a name="N7jOi"></a>
##### 2.安装可视化工具
[mongodb-compass-1.27.1-win32-x64.zip](https://www.yuque.com/attachments/yuque/0/2022/zip/2673847/1650199867389-42ed495a-d4f2-4c81-8aed-f029bb0e0ba8.zip?_lake_card=%7B%22src%22%3A%22https%3A%2F%2Fwww.yuque.com%2Fattachments%2Fyuque%2F0%2F2022%2Fzip%2F2673847%2F1650199867389-42ed495a-d4f2-4c81-8aed-f029bb0e0ba8.zip%22%2C%22name%22%3A%22mongodb-compass-1.27.1-win32-x64.zip%22%2C%22size%22%3A144370417%2C%22type%22%3A%22application%2Fx-zip-compressed%22%2C%22ext%22%3A%22zip%22%2C%22source%22%3A%22%22%2C%22status%22%3A%22done%22%2C%22mode%22%3A%22title%22%2C%22download%22%3Atrue%2C%22taskId%22%3A%22uccdef478-a285-4e99-aa00-544f74f909c%22%2C%22taskType%22%3A%22upload%22%2C%22id%22%3A%22u6bbad47d%22%2C%22card%22%3A%22file%22%7D)<br />展示界面<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199795911-e793ac37-aff7-4bd3-bf94-816e3f20e1c9.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=834&id=u9daba29c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1042&originWidth=1584&originalType=binary&ratio=1&rotation=0&showTitle=false&size=75857&status=done&style=none&taskId=u68ff34f5-f019-428e-82e2-75b2a634bd9&title=&width=1267.2)

<a name="Ef1QT"></a>
##### 3.测试连接数据库

- 在当前目录初始化命令: `npm init -y`

效果:<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199922603-1ca48dd5-f918-473f-8e50-4ee460df651c.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=46&id=u6896739a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=57&originWidth=426&originalType=binary&ratio=1&rotation=0&showTitle=false&size=4640&status=done&style=none&taskId=u4f785e2d-5172-4d7d-8b34-1808902ed76&title=&width=340.8) 

- 命令:` npm install mongoose`

![image.png](https://cdn.nlark.com/yuque/0/2022/png/2673847/1650199961026-67cd7e6b-cd40-4277-b4f4-20bf8c9f6e3e.png#clientId=u0691c219-2555-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=39&id=ue0396da7&margin=%5Bobject%20Object%5D&name=image.png&originHeight=49&originWidth=400&originalType=binary&ratio=1&rotation=0&showTitle=false&size=3219&status=done&style=none&taskId=u067dba55-c57d-4bf0-b115-f072cb3f545&title=&width=320)

- 编写一个js文件连接数据库
```javascript
//mongoose 第三方包,是nodejs 专门用于处理mongoDB的包
//01.下载包:npm install mongoose
const mongoose = require('mongoose');

//连接数据库
mongoose.connect('mongodb://localhost/mySchool').then((r)=>{
  console.log('数据库连接成功');
}).catch((err)=>{
  console.log('数据库连接成功',err);
})
  • 控制台测试

image.png

Stream流

读取流文件

var fs = require('fs')
var data = ''
var readStream = fs.createReadStream('input.txt')
readStream.setEncoding('utf-8')
readStream.on('data', function (_d) {
  data += _d
})
readStream.on('end', function () {
  console.log(data)
})
console.log('读取完毕')

写出流文件

var fs = require('fs')
var data = '我是老尚,今年18'
var writeStream = fs.createWriteStream('1.txt')

writeStream.write(data, 'utf-8')
writeStream.end()

writeStream.on('finsh', function () {
  console.log('写入完成')
})

模块系统

image.png

get / post 请求

get

image.png
image.png
代码:、

var http = require('http')
var util = require('util')
var url = require('url')

http
  .createServer(function (request, response) {
    response.writeHead(200, {
      'Content-Type': 'text/plain;charset=utf-8',
    })

    var _param = url.parse(request.url, true).query
    response.write('姓名' + _param.name)
    response.write('\n')
    response.write('年龄' + _param.age)
    response.end()
  })
  .listen(5642)

console.log('5642,已经启动')

image.png

post

image.png
代码:

var http = require('http')
var util = require('util')
var url = require('url')
var querystring = require('querystring')

var _formHtml =
  '<form method="post">' +
  '网址名:<input name = "name"><br>' +
  '网址URL:<input name="age"><br>' +
  '<input type="submit"> ' +
  '</form >'

http
  .createServer(function (req, res) {
    var body = ''
    req.on('data', function (_d) {
      body += _d
    })
    req.on('end', function () {
      body = querystring.parse(body)
      console.log(body)
      res.writeHead(200, {
        'Content-Type': 'text/html;charset=utf-8',
      })
      if (body.name) {
        res.write('姓名:' + body.name)
        res.write('<br/>')
        res.write('年龄:' + _param.age)
      } else {
        res.write(_formHtml)
      }
      res.end()
    })
  })
  .listen(5642)

console.log('5642,已经启动')

image.png
image.png
image.png

Express框架

image.png

Express框架的路由

image.png

Express框架的静态目录

image.png