nodejs接口是使用nodejs实现的包含方法、属性、索引器和事件作为成员对协定进行定义的引用类型。nodejs可以通过express模块来写接口。
视频教程推荐:node js教程
一个nodejs接口:
db部分公共
var mysql = require('mysql'); var dbMsg = { host : 'localhost', user : 'root', password : '123456', database : 'app_pro' } var connection = mysql.createConnection(dbMsg); connection.connect(); module.exports = connection;
app.js
var express = require('express'); var app = express(); var bodyParser = require('body-parser'); //获取数据库连接对象 var connection = require('./mysql/db'); //处理post字段请求 app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); //处理跨域请求 app.all("*", function(req, res, next) { res.header("Access-Control-Allow-Credentials", true); res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); res.header("Content-Type", "application/json;charset=utf-8"); next(); }); app.get('/',function(req,res){ res.send('请求home成功'); }) //用户登录 app.post('/user/login', (req,res) => { var name = req.body.username; var passwd = req.body.password; var userStr = `select * from user where username="${name}" and password="${passwd}"`; connection.query(userStr,function(err,result){ if(err){ throw err; }else{ res.send(result) } }) }) //用户注册 const port = 3001; app.listen(port, () => { console.log('Express server listening on port ' + port); });
想要了解更多相关知识,可访问 前端学习网站!!
以上就是nodejs接口是什么?的详细内容,更多请关注易知道|edz.cc其它相关文章!