GithubHelp home page GithubHelp logo

nodeserve's Introduction

说明&&运行方法

本项目是node.js 无任何框架用来模拟登录注册增删改查
前提想要查询数据必须得登录或者注册

  • 账号信息在根目录下的user.json
  • 查询的数据在data.json里
  • 起始文件在根目录下的server.js
npm i
node ./server.js

举例 api 如果要注册的话使用http://localhost:3000/api/register
{
 用户名  "username": "user1",
 密码    "password": "password1"
}

如果在直接请求data报错TypeError: res.status is not a function
可以尝试在浏览器控制台输入
fetch('http://localhost:3000/api/data', {
 method: 'GET',
 headers: {
   'Content-Type': 'application/json',
   'Authorization': `Bearer  你登录的token`
 },
})
建议用postman 请求接口比较稳一点

可使用接口

  • 登录接口
  • 注册接口
  • 获取当前用户的所有数据
  • 获取单个数据
  • 添加数据
  • 更新数据
  • 删除数据

登录接口

URL: /api/login
方法: POST
返回值: 包含getToken
postman参考值
POST   http://localhost:3000/api/login

注册接口

URL: /api/register
方法: POST
返回值: 包含所有数据的JSON数组以及token
postman参考值
POST   http://localhost:3000/api/login

获取所有数据=>单指当前登录用户的值

URL: /api/data
方法: GET
返回值: 包含所有数据的JSON数组

postman参考值
GET   http://localhost:3000/api/data

获取单个数据

URL: /api/data/:id
方法: GET
参数: id - 数据ID
返回值: 包含单个数据的JSON对象,如果找不到指定ID的数据则返回404

postman参考值
GET   http://localhost:3000/api/data/1678090271462

添加数据

URL: /api/data/adddata"
方法: POST
请求体: 包含新数据的JSON对象
返回值: 包含新数据及其ID的JSON对象
postman参考值
POST  http://localhost:3000/api/data/adddata
Body参考值
 {
    "name": "Alice",
    "age": 22
  }

更新数据

URL: /api/data/:putid
方法: PUT
参数: id - 数据ID
请求体: 包含要更新的数据字段及其值的JSON对象
返回值: 包含更新后的数据的JSON对象,如果找不到指定ID的数据则返回404

postman参考值
PUT   http://localhost:3000/api/data/1678090271461
Body参考值
{"age": "1678090271461"}

删除数据

URL: /api/data/:deleteid
方法: DELETE
参数: id - 数据ID
返回值: 包含被删除的数据的JSON对象,如果找不到指定ID的数据则返回404

postman参考值
Deleteid  http://localhost:3000/api/data/1678090271462

目录

  • server.js
  • data.json
  • users.json
  • routes/
    • dataRoutes.js
  • controllers/
    • dataController.js
    • authController.js
  • utils/
    • jwt.js
    • dataUtils.js

nodeserve's People

Stargazers

 avatar  avatar

Watchers

Panliangyi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.