首页 > 编程语言 >node restAPI 简单例子

node restAPI 简单例子

时间:2023-07-05 23:46:15浏览次数:44  
标签:node const restAPI app req courses 例子 res id

 

 

// 第一版,node的httpp
// const http = require('http');
// const server=http.createServer((req,res)=>{
//     if(req.url==='/'){
//         res.write('hello world')
//         res.end();
//     }
//     if(req.url==='/api/courses'){
//         res.write(JSON.stringify([1,2,3]))
//         res.end();
//     }
// })

// server.listen(3000)

// console.log('Listening on port 3000...')







// 第二版,node http库  express
const exp = require('constants');
const express=require('express')
const app =express();

app.use(express.json())

const courses=[
    {id:1,name:'course1'},
    {id:2,name:'course2'},
    {id:3,name:'course3'}
]

app.get('/',(req,res)=>{
    res.send('hello world')
})


app.get('/api/courses',(req,res)=>{
    res.send(courses)
})

//Joi 库可以做验证
app.post('/api/courses',(req,res)=>{
    if(!req.body.name || req.body.name.length<3){
        //400 Bad Request
        res.status(400).send('Name is required and ....')
    }
    const course={
        id:courses.length+1,
        name:req.body.name
    }
    courses.push(course)
    res.send(course)
})


app.get('/api/courses/:id',(req,res)=>{
    let course=courses.find(c=>c.id===parseInt(req.params.id))
    if(!course)//404
    res.status(404).send('the course with the give ID not found')
    res.send(course)
})

// app.get('/api/posts/:year/:month',(req,res)=>{
//     res.send(req.query)req.params,
// })


const port=process.env.PORT||3000
app.listen(port,()=>{
    console.log(`Listening on port ${port}...`)
})
// app.post()
// app.put()
// app.delete()

 

标签:node,const,restAPI,app,req,courses,例子,res,id
From: https://www.cnblogs.com/hechunfeng/p/17530633.html

相关文章

  • Linux 虚拟文件系统四大对象:超级块、inode、dentry、file之间关系
    转载:Linux虚拟文件系统四大对象:超级块、inode、dentry、file之间关系-一口Linux-博客园(cnblogs.com)一:文件系统1.什么是文件系统?操作系统中负责管理和存储文件信息的软件机构称为文件管理系统,简称文件系统。通常文件系统是用于存储和组织文件的一种机制,便于对文件进行......
  • Element-plus按需导入报错:Error: Cannot find module 'node:module'
    1.问题vue3项目使用ElementPlus组件库,配置按需导入:首先安装unplugin-vue-components和unplugin-auto-import这两款插件npminstall-Dunplugin-vue-componentsunplugin-auto-import然后按照文档在配置文件中进行相关配置;因为更改了配置文件,所以得重新启动项目--......
  • 利用ansible批量部署node客户端,并注册consul,实现主机自动发现
      1.在管理机器上搭建consul并上传 node_exoporter软件包,system服务配置文件,注册脚本 2.利用ansible对指定机器去分发软件包并启动服务,并curl注册到consul  编写为node-exporter.yml 3.prometheus配置consul地址,获取主机信息,自动发现并配合grafana展示  system......
  • vue项目报错:Node.js v18.16.1 error Command failed with exit code 1.
    原因:把node升级到了最新的长期支持版18.16.1,结果运行vue项目启动失败,报错如下:试了各种办法都解决不了,后面只能把node降级到16.20.1运行项目又可以启动了......
  • 使用 node 17以上版本运行项目报错--Error: error:0308010C:digital envelope routine
    一、起因#由于电脑重装系统,重新下载nodejs,自然更新到最新版本18,之前的版本才16。更新到最新nodejs版本后,运行vue文件,报错:this[kHandle]=new_Hash(algorithm,xofLen);^Error:error:0308010C:digitalenveloperoutines::unsupported   二、探索......
  • linux源码解读(三):文件系统——inode【转】
    转自:https://www.cnblogs.com/theseventhson/p/15622853.html众所周知,计算机系统在掉电后也能存储数据的就是磁盘了,所以大量数据大部分时间是存放在磁盘的;现在新买的PC,磁盘从数百G到1TB不等;服务器的磁盘从数十TB到上百TB,这么大的存储空间,该怎么高效地管理和使用了?站在硬件角度,cpu......
  • Linux struct inode结构【转】
    转自:https://www.cnblogs.com/wanghetao/archive/2012/05/28/2521675.html*索引节点对象由inode结构体表示,定义文件在linux/fs.h中*/structinode{      structhlist_node    i_hash;          /*哈希表*/      structlist_head  ......
  • nodejs——linux安装环境
    yum安装nodejsv18.16.1model:unknown,wordcount:7,tokenestimate:14   在yum仓库中,可能没有提供Node.js18.16.1的特定版本。通常,yum仓库中提供的是最新稳定版本的软件包。如果您需要安装特定版本的Node.js,可以考虑使用Node版本管理工具(例如nvm......
  • nodeselector 节点选择---马哥教育
    创建一个将被调度到你选择的节点的Pod 此Pod配置文件描述了一个拥有节点选择器 disktype:ssd 的Pod。这表明该Pod将被调度到有 disktype=ssd 标签的节点。https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/assign-pods-nodes/ kubectl apply ......
  • [GPT] nodejs 有哪些类似 jquery 语法的 html 解析库
     在Node.js中,有一些类似jQuery语法的HTML解析库可供选择。 以下是其中几个常用的库:1.Cheerio:Cheerio是一个快速、灵活且易于使用的HTML解析库,它提供了类似于jQuery的语法和API。你可以使用Cheerio来在Node.js中解析和操作HTML文档。 2.jsdom:jsdom是一个基于Node.......