nodejs写接口

Dear 丶 2024-04-19 00:58 118阅读 0赞

在项目根目录下:“npm init” 初始化nodejs项目,生成package.json文件

npm install express -S (安装express框架)

在根目录下新建文件app.js

  1. var express = require('express');//引入express模块
  2. var app = express();
  3. //定义方法
  4. app.get('/',function(req,res){
  5. res.send('HellowWorld')
  6. });
  7. //定义端口,此处所用为3000端口,可自行更改
  8. var server = app.listen(3000,function(){
  9. console.log('runing 3000...');
  10. })

创建接口响应路径 (get)

  1. var express = require('express');//引入express模块
  2. var app = express();
  3. //定义方法
  4. app.get('/',function(req,res){
  5. res.send('HellowWorld')
  6. });
  7. //响应接口
  8. app.get('/list/product',function(req,res){
  9. //数据
  10. let result={
  11. err:0,
  12. msg:'ok',
  13. data:{
  14. "name":"huawei",
  15. "price":"1999",
  16. "title":"huawei huawei huawei",
  17. "id":"1"
  18. }
  19. }
  20. res.send(result)
  21. })
  22. //定义端口,此处所用为3000端口,可自行更改
  23. var server = app.listen(3000,function(){
  24. console.log('runing 3000...');
  25. })

运行命令 node .\app.js 启动项目

在浏览器中输入 http://127.0.0.1:3000/list/product 即可看到数据

在这里插入图片描述

发送post请求

  1. // 1.导入http模块
  2. var http = require('http');
  3. // 2. 发送请求
  4. var post_data = JSON.stringify({
  5. 'width':100,
  6. 'height':100
  7. }) // post所需要传递的数据
  8. var options = {
  9. hostname:'www.baidu.com', //此处不能写协议,如 : http://,https:// 否则会报错
  10. port:5001,
  11. path:'/api',
  12. method:'POST',
  13. headers: {
  14. 'Content-Type':'application/json',
  15. 'Content-Length': post_data.length
  16. }
  17. }
  18. var req = http.request(options,function(res){
  19. console.log('STATUS:'+res.statusCode);
  20. var body = ''
  21. res.setEncoding('utf8');
  22. res.on('data',function(chunk){
  23. body += chunk;
  24. })
  25. res.on('end', function(){
  26. process.nextTick(() =>{
  27. body = JSON.parse(body)
  28. try{
  29. console.log(body) // 请求到的数据
  30. } catch (err) {
  31. console.log('compress error', err)
  32. }
  33. })
  34. })
  35. res.on('error', function(err) {
  36. console.log('api error', err)
  37. })
  38. })
  39. // write data to request body
  40. req.write(post_data)
  41. req.end()

(附)案例:返回图片url给前端渲染

  1. var express = require('express');//引入express模块
  2. var app = express();
  3. // 开放根目录下image文件夹,使得外网可以访问
  4. app.use('/image',express.static('./image'));
  5. //定义方法
  6. app.get('/',function(req,res){
  7. res.send('HellowWorld')
  8. });
  9. app.get('/home',function(req,res){
  10. let data=[
  11. { 'img1': 'http://127.0.0.1:3000/image/J1.jpg'},
  12. { 'img2': 'http://127.0.0.1:3000/image/J2.png'},
  13. { 'img3': 'http://127.0.0.1:3000/image/J3.png'}
  14. ]
  15. res.send(data)
  16. });
  17. //定义端口,此处所用为3000端口,可自行更改
  18. var server = app.listen(3000,function(){
  19. console.log('runing 3000...');
  20. })

可参考https://www.jianshu.com/p/c76279494a2f

发表评论

表情:
评论列表 (有 0 条评论,118人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nodejs接口

    在项目根目录下:“npm init” 初始化nodejs项目,生成package.json文件 npm install express -S (安装express框...