Node-express项目--个人简历:搭建当前用户的个人信息接口Profile

总结

根据路线图上的重点去进行有针对性的学习,在学习过程中,学会写笔记,做总结。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

这里分享一些前端学习笔记:

  • html5 / css3 学习笔记

  • JavaScript 学习笔记

  • Vue 学习笔记

}

})

module.exports = Profile = mongoose.model(“profile”,profileSchema);

2.在router/api文件夹下新建Profile的路由文件profile.js,先写一个简单的test 案例,测试路由是否正常

const express = require(“express”);

const router = express.Router();

router.get(‘/test’, (req, res) => {

res.json({ msg: “profile works” })

})

3.在index.js文件中引入该路由

const profile = require(“./routes/api/profile”);

app.use(“/api/profile”,profile);

4.路由测试(记得带上没有过期的token)

在这里插入图片描述

新增、修改个人信息


1.用户输入个人信息。用postman模拟(记得带上未过期的token):

在这里插入图片描述

2.在profile.js文件中,把用户输入的信息全部收集存储到profileFields中

//$route POST api/profile/

//@desc add or modify profile msg

//@access private

router.post(“/”, passport.authenticate(“jwt”, { session: false }), (req, res) => {

const errors = {}

const profileFields = {}

profileFields.user = req.user.id;

// 字符串

if (req.body.handle) {

profileFields.handle = req.body.handle;

}

if (req.body.status) {

profileFields.status = req.body.status;

}

// 字符串数组

if (typeof req.body.skills !== “undefined”) {

profileFields.skills = req.body.skills.split(“,”);

}

// 对象-experience

// profileFields.experience = {}

// 此处不用手动创建空对象experience,否则,没有输入experience也会填充current,id,得到无意义数据experience:[{current:true, _id:12345678}]

if (req.body.current) {

profileFields.experience.current = req.body.current;

}

if (req.body.title) {

profileFields.experience.title = req.body.title;

}

if (req.body.company) {

profileFields.experience.company = req.body.company;

}

if (req.body.location) {

profileFields.experience.location = req.body.location;

}

if (req.body.from) {

profileFields.experience.from = req.body.from;

}

if (req.body.to) {

profileFields.experience.to = req.body.to;

}

if (req.body.description) {

profileFields.experience.description = req.body.description;

}

// 对象 social

if (req.body.wechat) {

profileFields.social.wechat = req.body.wechat;

}

if (req.body.QQ) {

profileFields.social.QQ = req.body.QQ;

}

if (req.body.CSDN) {

profileFields.social.CSDN = req.body.CSDN;

}

})

3.如果存在个人信息,则更新信息;如果没有,就新增信息

Profile.findOne({ user: req.user.id })

.then(profile => {

console.log(“profile”,profile);

if (profile) {

//用户信息存在,执行更新方法

Profile.findOneAndUpdate({ user: req.user.id }, { $set: profileFields }, { new: true })

.then(profile => res.json(profile))

} else {

//用户信息不存在,执行创建方法

Profile.findOne({ handle: profileFields.handle }).then(profile => {

new Profile(profileFields).save()

.then(profile => res.json(profile));

});

}

})

.catch(err => { res.status(404).json(err) })

4.postman测试结果

第一次send,由于没有添加过个人信息,因此此次是执行新增操作:

在这里插入图片描述

数据库查看:

在这里插入图片描述

将handle的值改成"test",再次send,此次是更新个人信息:

在这里插入图片描述

数据库查看:

在这里插入图片描述

查询个人信息


1.get获取当前用户的个人信息,如果有,则显示,没有则提示用户没有个人信息

//$route GET api/profile/

//@desc return user profile msg

//@access private

router.get(“/”, passport.authenticate(“jwt”, { session: false }), (req, res) => {

// 能够操作此接口,代表用户是存在的,就会有用户id

// req.user.id是从认证获得的

profileFields.user = req.user.id;

// req.user.id是关联桥梁

Profile.findOne({ user: req.user.id })

.then(profile => {

if (!profile) {

errs.noprofile = “用户信息不存在”

return res.status(404).json(errs)

}

res.json({ profile })

})

.catch(err => { res.status(404).json(err) })

})

2.postman测试结果,同样要记得在Header里带上token:

在这里插入图片描述

验证用户输入的信息格式


1.创建验证:在validation文件夹下新建文件profile.js

需要验证的数据有:

  • 在Profile.js定义数据格式时,曾指定handle、status和skills为必填项

  • 在定义数据格式时,曾指定handle长度不能超过40位

  • CSDN链接需要是一个正确的url

const Validator = require(‘validator’);

const isEmpty = require(“./empty”);

module.exports = function validateProfileInput(data) {

let errors = {};

// 在Profile定义数据时指明,handle status skills是必填的三个字段

//如果输入空(没有输入),需要将其改为空字符串

data.handle = !isEmpty(data.handle) ? data.handle : ‘’;

data.status = !isEmpty(data.status) ? data.status : ‘’;

data.skills = !isEmpty(data.skills) ? data.skills : ‘’;

// handle定义时指明,最大长度为40

if (!Validator.isLength(data.handle, { min: 2, max: 40 })) {

errors.handle = “handle的长度不能小于2位并且不能大于40位!”;

}

// handle不能为空

if (Validator.isEmpty(data.handle)) {

errors.handle = “handle不能为空”;

}

数据结构与算法

这一块在笔试、面试的代码题中考核较多,其中常考的数据结构主要有:数组、链表、队列、栈、Set、Map、哈希表等,不同数据结构有不同的方法以及储存原理,这些算是技术岗的必备知识。算法部分主要分为两大块,排序算法与一些其他算法题

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

排序算法根据考频高低主要有:快速排序、归并排序、堆排序、冒泡排序、插入排序、选择排序、希尔排序、桶排序、基数排序、Timsort这十种,这类考核点要么是算法的时间、空间复杂度、稳定度,要么是直接手写代码,故在理解算法原理的同时,对JS语言版的排序算法代码也要加强记忆。

  • 二叉树层序遍历
  • B 树的特性,B 树和 B+树的区别
  • 尾递归
  • 如何写一个大数阶乘?递归的方法会出现什么问题?
  • 把多维数组变成一维数组的方法
  • 知道的排序算法 说一下冒泡快排的原理
  • Heap 排序方法的原理?复杂度?
  • 几种常见的排序算法,手写
  • 数组的去重,尽可能写出多个方法
  • 如果有一个大的数组,都是整型,怎么找出最大的前 10 个数
  • 知道数据结构里面的常见的数据结构
  • 找出数组中第 k 大的数组出现多少次,比如数组【1,2, 4,4,3,5】第二大的数字是 4,出现两次,所以返回 2
  • 合并两个有序数组
  • 给一个数,去一个已经排好序的数组中寻找这个数的位 置(通过快速查找,二分查找)

Timsort这十种,这类考核点要么是算法的时间、空间复杂度、稳定度,要么是直接手写代码,故在理解算法原理的同时,对JS语言版的排序算法代码也要加强记忆。

  • 二叉树层序遍历
  • B 树的特性,B 树和 B+树的区别
  • 尾递归
  • 如何写一个大数阶乘?递归的方法会出现什么问题?
  • 把多维数组变成一维数组的方法
  • 知道的排序算法 说一下冒泡快排的原理
  • Heap 排序方法的原理?复杂度?
  • 几种常见的排序算法,手写
  • 数组的去重,尽可能写出多个方法
  • 如果有一个大的数组,都是整型,怎么找出最大的前 10 个数
  • 知道数据结构里面的常见的数据结构
  • 找出数组中第 k 大的数组出现多少次,比如数组【1,2, 4,4,3,5】第二大的数字是 4,出现两次,所以返回 2
  • 合并两个有序数组
  • 给一个数,去一个已经排好序的数组中寻找这个数的位 置(通过快速查找,二分查找)

[外链图片转存中…(img-1Lw3dwyf-1715154720012)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值