从零开始nodejs之博客项目

1,205 阅读3分钟

初始化node项目

1.创建项目文件夹

2.命令行输入npm init 或者npm init -y

3.生成package.json文件

npm init -y

-y 的含义:yes的意思,在init的时候省去了敲回车的步骤,生成的默认的package.json

了解http请求

http请求概述:当浏览器请求域名时,所要干的事情

img

三次握手都干了什么?

第一次握手>客户端询问服务器你是否可用。

第二次握手>服务器告诉客户端,可用。

第三次握手>客户端告诉服务器,好,我要开始访问了。

node.js处理http请求

首先我们先学习一下,node获取客户端请求的一个简单示例

img

通过该方法,我们就可以抓取客户端的请求,并且做出响应。

1.get请求和querystring

node.js处理get请求

img

服务器接收gei请求,处理请求url地址后的参数(即querystring就是,?后面的参数)

querystring.parse( ) 方法

列:querystring.parse('foo=bar&baz=qux&baz=quux&corge') 输出:{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }

node.js处理POST请求

1.创建一个server,用于接收客户端post请求传递的参数

img

2.使用postman工具,模拟客户端向服务器发送一个post(客户端向服务器提交数据)请求

img

3.处理post请求时,服务器控制台返回的结果

img

nodejs小综合案例,使用postman模拟发送post或者get请求

1.服务器端app.js代码

img

img

2.使用postman发送get请求

img

3.使用postman发送post请求

img

nodejs原生开发博客项目开始

搭建开发环境

初始化文件目录

img

config-dev.js------node一些固定配置

img

app.js -------将回调抽离出来,单独写,此处实现业务逻辑(类似jsp三层架构中web层中的servlet)

img

全局安装nodemon

以前,我们开发一个node后端服务时,每次更改文件,均需重启一下,服务才能生效。这使我们的开发效率降低了很多。nodemon的出现,可以随时监听文件的变更,自动重启服务,我们开发时只需关注代码即可,不再需要手动重启服务。

~在没有安装nodemon之前,我们通常这样运行

index.js

console.log('xiaokeqi')

运行它,须通过命令:

node index.js

我们若在index.js文件中再新增一行

index.js

console.log('xiaokeqi')
console.log('xiaoqi')

我们则需要再次手动运行

node index.js

~而出现nodemon后,我们只需运行

nodemon ./index.js 或者 nodemon 文件路径/index.js

即可。其会找到当前路径下的index.js,并watch其变更,自动重启服务。我们只需要安心写代码逻辑就好,再也不需要关注服务了!

nodemon此处参考原文:zhuanlan.zhihu.com/p/96720675

接口设计

接口搭建

1.在src目录下新建router文件夹用于保存接口文件,目录结构如下

img

2.新建 blog.js 文件中存放,关于博客增删查改的接口

代码如下:

const querustring = require('querystring')
const blogApi = require('./src/router/blog')
const userApi = require('./src/router/user')

// 业务逻辑代码
const callBlack = (req, res) => {
    // 设置返回格式为json格式
    res.setHeader('Content-type', 'application/json')

    //    处理blog.js路由
    const blogBlackData = blogApi(req, res)
    if (blogBlackData) {
        res.end(JSON.stringify(blogBlackData))
        return
    }
    //    处理user.js路由
    const userBlackData = userApi(req, res)
    if (userBlackData) {
        res.end(JSON.stringify(userBlackData))
        return
    }


    // 未命中路由, 返回404
    res.writeHead(404, {
        "content-type": "text/plain"
    })
    res.write("404 Not Found\n")
    res.end()
}

module.exports = callBlack

3.新建 user.js 文件中存放,关于用户登录的接口

const userApi = (req, res) => {
    const url = req.url
    const method = req.method
    const query = url.split('?')[1]
    const path = url.split('?')[0]

    // 用户登录
    if (method === 'POST' && path === '/api/user/login') {
        return {
            msg: '用户登录成功'
        }
    }
}
module.exports = userApi

3.在业务逻辑代码中导入blog.js和user.js文件,并使用

const querustring = require('querystring')
const blogApi = require('./src/router/blog')
const userApi = require('./src/router/user')

// 业务逻辑代码
const callBlack = (req, res) => {
    // 设置返回格式为json格式
    res.setHeader('Content-type', 'application/json')

    //    处理blog.js路由
    const blogBlackData = blogApi(req, res)
    if (blogBlackData) {
        res.end(JSON.stringify(blogBlackData))
        return
    }
    //    处理user.js路由
    const userBlackData = userApi(req, res)
    if (userBlackData) {
        res.end(JSON.stringify(userBlackData))
        return
    }


    // 未命中路由, 返回404
    res.writeHead(404, {
        "content-type": "text/plain"
    })
    res.write("404 Not Found\n")
    res.end()
}

module.exports = callBlack

4.使用postman工具访问localhost:3000端口,检测接口是否搭建成功

img

返回数据msg,接口访问成功