为unity写简单的web接口(nodejs)
程序员文章站
2022-05-16 23:18:53
//引入express 创建服务器var express = require('express');var app = express();// 需要对表单数据进行解析的,安装bodyParservar bodyParser = require('body-parser'); //解析函数app.use(bodyParser.json()); //json请求app.use(bodyParser.urlencoded({ extended:true})); //表单请求....
//引入express 创建服务器
var express = require('express');
var app = express();
// 需要对表单数据进行解析的,安装bodyParser
var bodyParser = require('body-parser'); //解析函数
app.use(bodyParser.json()); //json请求
app.use(bodyParser.urlencoded({ extended:true})); //表单请求
// 设置跨域访问
app.all('*',function (req,res,next) {
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("X-Powered-By",'3.2.1');
res.header("Content-Type","application/json;charset=utf-8");
next();
})
var mysql = require('mysql');
var info = [
{
data:123,
num:1,
age:11
},{
data:456,
num:2,
age:22
}
];
var connection = mysql.createConnection({
host : '192.168.1.121',//主机名或IP地址
port : '3306',//端口号
user : 'root', //用户名
password : '123456',//密码
database : 'zkstest'//数据库名
});
conect();
function conect() {
connection.connect(function (err) {
if (err) {
console.error('error connecting:' + err.stack)
close();
}
console.log('connected as id ' + connection.threadId);
})
}
function close(){
connection.end();
}
// 配置接口api
app.get('/api11',function (req,res) {
res.status(200),
res.json(info)
// res.send(info)
})
app.post('/getSceneData',function (req,res) {
connection.query("SELECT * FROM seu_sceneData WHERE DEP_PARENT="+req.param('DEP_PARENT') , function (error, results, fields) {
if (error) throw error;
//console.log( req.param('DEP_PARENT'));
res.json(results);
});
})
// 配置服务端口
var server = app.listen(3001,function(){
// var host = server.address().address;
// var port = server.address().port;
// console.log('listen at http://%s%s',host,port);
console.log('服务启动');
})
本文地址:https://blog.csdn.net/qq_34980278/article/details/108979793
上一篇: 好笑的是Ta们总能突破你的想象力