nodejs笔记【3项目】

这篇博客详细记录了使用Node.js进行项目开发的过程,包括启动服务、配置不同接口,如博客列表和详情,处理GET和POST请求,以及模块的暴露。还探讨了接口在前后端交互中的作用,并列举了接口列表。
摘要由CSDN通过智能技术生成

前言:准备阶段


敲命令来生成package.json文件

npm init -y

1. 项目入口文件


1.1 启动http服务,引入http模块

// 项目入口文件
const http = require("http");
// 创建 http 服务
const app = http.createServer((req,res)=>{
    res.end('ok');
});
// 处理网络请求
// app.on((req,res)={});
// 启动服务
app.listen(3000,()=>{
    console.log("Server is running at http://127.0.0.1:3000");
})

在这里插入图片描述

1.2 修改

// 项目入口文件
const http = require("http");
// 创建 http 服务
const app = http.createServer((req,res)=>{
    let result={
        name:'skd',
        age:20
    }
    // result 是object类型,我们需要把他转成字符串类型
    res.end(JSON.stringify(result))
});
// 处理网络请求
// app.on((req,res)={});
// 启动服务
app.listen(3000,()=>{
    console.log("Server is running at http://127.0.0.1:3000");
})

在这里插入图片描述

1.3 添加响应头

// 设置返回格式:JSON
res.setHeader("content-type", 'application/json')

在这里插入图片描述

在这里插入图片描述


1.4 配置不同接口

1.4.1 博客列表

在这里插入图片描述

1.4.2 博客详情

在这里插入图片描述

1.4.3 代码精简
// 项目入口文件
const http = require("http");
// 创建 http 服务
const app = http.createServer((req, res) => {
    // 设置返回格式:JSON
    res.setHeader("content-type", 'application/json');
    let msgResult = null;
    if(req.url=='/api/blog/list'){
        msgResult ={msg:'博客列表'}
    }else if(req.url=='/api/blog/detail'){
        msgResult ={msg:'博客详情'}
    }
    res.end(JSON.stringify(msgResult))
});
// 处理网络请求
// app.on((req,res)={});
// 启动服务
app.listen(3000, () => {
    console.log("Server is running at http://127.0.0.1:3000");
})

1.5 post与get请求类型不同

代码:

// 项目入口文件
const http = require("http");
// 创建 http 服务
const app = http.createServer((req, res) => {
    // 设置返回格式:JSON
    res.setHeader("content-type", 'application/json');
    let msgResult = null;
    if(req.url=='/api/blog/list' && req.method=='GET'){
        msgResult ={msg:'博客列表'}
    }else if(req.url=='/api/blog/detail' && req.method=='GET'){
        msgResult ={msg:'博客详情'}
    }else if(req.url=='/api/blog/new' && req.method=='POST'){
        msgResult ={msg:'新增博客'}
    }
    res.end(JSON.stringify(msgResult))
});
// 处理网络请求
// app.on((req,res)={});
// 启动服务
app.listen(3000, () => {
    console.log("Server is running at http://127.0.0.1:3000");
})

在这里插入图片描述

1.6 postman软件可实现不同请求类型的转换

// 项目入口文件
const http = require("http");
// 创建 http 服务
const app = http.createServer((req, res) => {
    console.log(req.method);
    // 设置返回格式:JSON
    res.setHeader("content-type", 'application/json');
    let method=req.method
    let pathname=req.url
    let msgResult = null;
    if(pathname=='/api/blog/list' && method=='GET'){
        msgResult ={msg:'博客列表'}
    }else if(pathname=='/api/blog/detail' && method=='GET'){
        msgResult ={msg:'博客详情'}
    }else if(pathname=='/api/blog/new' && method=='POST'){
        msgResult ={msg:'新增博客'}
    }else if(pathname=='/api/blog/update' && method=='POST'){
        msgResult ={msg:'更新博客'}
    }else if(pathname=='/api/blog/del' && method=='POST'){
        msgResult ={msg:'删除博客'}
    }
    res.end(JSON.stringify(msgResult))
});
// 处理网络请求
// app.on((req,res)={});
// 启动服务
app.listen(3000, () => {
    console.log("Server is running at http://127.0.0.1:3000");
})

在这里插入图片描述

1.7 记录

// 项目入口文件
const url = require('url');
const http = require("http");
// 创建 http 服务
const app = http.createServer((req, res) => {
    
    // 设置返回格式:JSON
    res.setHeader("content-type", 'application/json');
    // 使用 URL 模块对 req.url 进行封装
    let myUrl = new URL(req.url,"http://127.0.0.1:3000/")
    let method=req.method
    let pathname=myUrl.pathname
    console.log(pathname);
    let msgResult = null;
    if(pathname=='/api/blog/list' && method=='GET'){
        msgResult ={msg:'博客列表'}
    }else if(pathname=='/api/blog/detail' && method=='GET'){
        msgResult ={msg:'博客详情'}
    }else if(pathname=='/api/blog/new' && method=='POST'){
        msgResult ={msg:'新增博客'}
    }else if(pathname=='/api/blog/update' && method=='POST'){
        msgResult ={msg:'更新博客'}
    }else if(pathname=='/api/blog/del' && method=='POST'){
        msgResult ={msg:'删除博客'}
    }
    res.end(JSON.stringify(msgResult))
});
// 处理网络请求
// app.on((req,res)={});
// 启动服务
app.listen(3000, () => {
    console.log("Server is running at http://127.0.0.1:3000");
})

在这里插入图片描述

1.8 exports可以暴漏模块内部成员

在这里插入图片描述

a.js页面
/**
 * 在模块内容部定义的变量或者函数,默认情况下是无法在模块外部访问的
 * 可以使用 expors 或者 module.exports 暴漏模块内部成员
 * 可以将 exports 看成一个空对象 {},所以 expors.a=a 代码其实相当于
 * 执行了如下代码 let exports={}; exports.n=a
 */
let a=10;
exports.n=a
b.js页面
/**
 * node.js 中模块分为系统模块、第三方模块,还有开发者自己开发的模块
 * require 引入模块的时候
 * 1、所有模块可以省略 .js 后缀
 * 2、如果是系统模块或者第三方模块,引入时,只需要写模块名称即可。
 * 3、如果是自定义模块,必须书写路径。原因在于:如果不书写路径,node.js 回去 node_modules 目录下寻找
 */
const module_a = require('./a')
console.log(module_a);

在这里插入图片描述

1.9 module.exports 也是可以暴漏模块内部成员的

两者区别区别:

exportsmodule.exports的别名(地址引用关系),导出对象最终以module.exports为准

在这里插入图片描述

2. 最终版

app.js页面

// 项目入口文件
const url = require('url');
const http = require("http");
// 引入 blog.js 模块
const blog_module=require('./src/router/blog')
// 创建 http 服务
const app = http.createServer((req, res) => {
    console.log(blog_module);
    // 设置返回格式:JSON
    res.setHeader("content-type", 'application/json');
    let msgResult = blog_module.handlerBlog(req)
    res.end(JSON.stringify(msgResult))
});
// 处理网络请求
// app.on((req,res)={});
// 启动服务
app.listen(3000, () => {
    console.log("Server is running at http://127.0.0.1:3000");
})

blog.js页面

const handlerBlog=(req)=> {
    // 使用 URL 模块对 req.url 进行封装
    let myUrl = new URL(req.url, "http://127.0.0.1:3000/")
    let method = req.method
    let pathname = myUrl.pathname
    console.log(pathname);
    let msgResult = null;
    if (pathname == '/api/blog/list' && method == 'GET') {
        msgResult = {msg: '博客列表'}
    } else if (pathname == '/api/blog/detail' && method == 'GET') {
        msgResult = {msg: '博客详情'}
    } else if (pathname == '/api/blog/new' && method == 'POST') {
        msgResult = {msg: '新增博客'}
    } else if (pathname == '/api/blog/update' && method == 'POST') {
        msgResult = {msg: '更新博客'}
    } else if (pathname == '/api/blog/del' && method == 'POST') {
        msgResult = {msg: '删除博客'}
    }
    return msgResult;
}
// 暴漏 handlerBlog 方法
module.exports={
    handlerBlog
}

在这里插入图片描述

在这里插入图片描述

3. 开发接口


接口(api):前端使用ajax方式请求的地址,后端根据用户请求的这个地址,进行响应的处理,并向前端返回响应的数据。接口将前端和后端联系起来

3.1 接口列表

描述接口方法url参数备注
获取博客列表/api/blog/listgetauthor、keyword(搜索关键字)1)如果参数为空,则查询所有博客信息、如csdn首页;2)如果keyword为空,则查询某个用户的所有博客;3)如果author为空,则查询符合某个条件的所有博客,与用户无关
获取一篇博客内容/api/blog/detailgetid
新增一篇博客/api/blog/newpostpost中有新增信息
更新一篇博客/api/blog/updatepostidpostData中有更新的内容
删除一篇博客/api/blog/delpostid
登录/api/user/loginpostpostData中有用户名和密码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值