第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【九月打卡】第4天 前端工程師 Node.js基礎(chǔ)

標(biāo)簽:
Node.js Html5

每天一遍,防止颓废。


课程名称:前端工程师2022
课程章节:Node.js基础入门 2161
讲师:双越
课程j介绍:

本课程重点介绍服务端开发和前端开发思路上的区别,为后续的服务端开发做一个基础的铺垫。


课程内容

【第1章】
双越老师先给我们讲了这门课的阶段内容的课程安排。

认识req和res

手写server

const http = require('http')
const server = http.createServer(() => {
    console.log('已接收到http请求');
})
server.listen(3000)
console.log('server启动成功 http://127.0.0.1:3000');

npm

npm init
npm i nodemon --save-dev

package.json

{
  "name": "test2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev":"nodemon index.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^2.0.19"
  }
}
  • nodejs如何监听https请求
const http = require('http')

const server = http.createServer(() => {
    console.log('已接收到http请求');
})

// 监听端口号
server.listen(3000)
  • 如何拿到req和res

req即Request,res即Response,是http请求的关键

// 传两个参数
const server = http.createServer((req,res) => {
  // 返回信息给前端
  res.end('Hello Word');
})
  • 如何使用req和res

req.url:返回前端访问的url

res.end():返回内容给前端

路由

路由和url

  • GET/api/list路由-> axios.get(’/api/list?a=1’)

  • POST/api/create路由->axios.get(’/api/create’,{…})

  • 路由是规则,url是具体形式

定义一个路由

  • 从req中获取url和method

  • 判断method是否符合

  • 看url是否符合

const http = require('http')
// 传两个参数
const server = http.createServer((req, res) => {
    const url = req.url;
    const path = req.url.split('?')[0]
    const method = req.method;
    // 模拟获取留言列表
    if (path == '/api/list' && method == 'GET') {
        res.end('api')   
    } else {
        res.end('404')
    }
    // 模拟创建留言
    if (path == '/api/create' && method == 'POST') {
        res.end('Create')
    } else {
        res.end('404')
    }
})

测试POST路由需要借助工具-postman
网址:https://www.postman.com/

图片描述
图片描述
图片描述

心得体会

双越老师给我们讲了如何获取http请求,Request请求,Respond返回,如何监听请求巧妙的应用res.end,req.url,req.method来判断做路由来判断返回的内容,开始有点难了起来,然后今天学习有点晚,明天继续接着冲

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消