热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

nodejs接口指的是什么

web前端|前端问答nodejsweb前端-前端问答泛解析站群源码,vscode判断符号合并,ubuntu连接网络,tomcat绑定域名映射,触角爬虫,php用户名密码验证,seo

web前端|前端问答nodejs接口指的是什么
nodejs
web前端-前端问答
泛解析站群源码,vscode判断符号合并,ubuntu 连接网络,tomcat绑定域名映射,触角爬虫,php 用户名密码验证,seo教程免费视频,企业网站.net,产品发布的网页模板lzw
本文操作环境:Windows10系统、nodejs 12.19.0版、Dell G3电脑。
影视vip源码分销,ubuntu如何安装yaf,家里出现爬虫汇总,php写法{,钱库seo推广lzw
nodejs接口指的是什么
phpsns源码,日常ubuntu的使用,爬虫插件怎么设置,php <<<符号,seo布局结构lzw
接口(软件类接口)是指对协定进行定义的引用类型。其他类型实现接口,以保证它们支持某些操作。接口指定必须由类提供的成员或实现它的其他接口。与类相似,接口可以包含方法、属性、索引器和事件作为成员。

nodejs接口是使用nodejs实现的包含方法、属性、索引器和事件作为成员对协定进行定义的引用类型。nodejs可以通过express模块来写接口。

nodejs如何写接口?

可以使用express框架,配合moment(时间格式化)中间件+cors中间件(跨域用)+body-parser(解析表单)+mysql中间件写一个接口。

Express是一个保持最小规模的灵活的 Node.js Web应用程序开发框架,为Web和移动应用程序提供一组强大的功能。node可以使用express框架中的app.get与app.post等方法连接对象进行操作。

直接上代码:

const express = require('express');const app = express();const moment = require('moment') //导入cors模块,该模块为跨域所用const cors = require('cors');app.use(cors());//解析表单的插件const bodyParser = require('body-parser');app.use(bodyParser.urlencoded({ extended: false }))//创建数据库连接对象const mysql = require('mysql');const cOnn= mysql.createConnection({ host: '127.0.0.1',//数据库地址 user: 'root',//账号 password: 'root',//密码 database: 'mydb_12_9',//库名 multipleStatements: true //允许执行多条语句})

get表中的数据:

app.get('/api/getlist', (req, res) => { const sqlStr = 'select * from zdcx ' conn.query(sqlStr, (err, results) => { if (err) return res.json({ err_code: 1, message: '资料不存在', affextedRows: 0 }) res.json({ err_code: 200, message: results, affextedRows: results.affextedRows }) })})

按条件查询:

app.get('/api/getlistdetl', (req, res) => { const number = req.query.number const sqlStr = 'select * from zdcx where number=?' conn.query(sqlStr, number, (err, results) => { if (err) return res.json({ err_code: 1, message: '资料不存在', affextedRows: 0 }) res.json({ err_code: 200, message: results, affextedRows: results.affextedRows }) })})

添加数据:

//添加app.post('/api/addcard', (req, res) => { const user = req.body user.ctime = moment().format('YYYY-MM-DD HH:mm:ss')//格式化日期 const sqlStr = 'insert into bank set ?' conn.query(sqlStr, user, (err, results) => { if (err) return res.json({ err_code: 1, message: err, affectedRows: 0 }) res.json({ err_code: 0, message: '恭喜成功', affectedRows: results.affectedRows }) })})

端口监听:

app.listen(3000, () => { console.log('正在监听端口3000,http://192.168.1.114:3000'); //192.168.1.114换成你的ip,本机ip查询用cmd=>ipconfig})

运行就直接node 你的文件名 就行了

《nodejs视频教学》


推荐阅读
author-avatar
83984246_42dbe6
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有