sse,html测试
parent
97e1a6202e
commit
9db91c2c10
@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
const mysql = require('mysql2');
|
||||||
|
|
||||||
|
const pool = mysql.createPool({
|
||||||
|
host: 'localhost',
|
||||||
|
user: 'root',
|
||||||
|
password: 'Skyinno251,',
|
||||||
|
database: 'appserver',
|
||||||
|
connectionLimit: 10 // 连接池最大连接数
|
||||||
|
});
|
||||||
|
|
||||||
|
pool.query('SELECT * FROM user', (err, rows) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('Error querying database:', err);
|
||||||
|
//res.status(500).send('Internal Server Error');
|
||||||
|
} else {
|
||||||
|
//res.json(rows);
|
||||||
|
}
|
||||||
|
});
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
const oracledb = require('oracledb');
|
||||||
|
|
||||||
|
// 设置数据库连接参数
|
||||||
|
const config = {
|
||||||
|
user: 'system',
|
||||||
|
password: '452131wW',
|
||||||
|
connectString : "192.168.3.29:1521/orcl" // 或者直接使用 'hostname:port/sid'
|
||||||
|
};
|
||||||
|
|
||||||
|
async function run() {
|
||||||
|
let connection;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 获取数据库连接
|
||||||
|
connection = await oracledb.getConnection(config);
|
||||||
|
console.log('Connection established.');
|
||||||
|
|
||||||
|
// 执行查询
|
||||||
|
const result = await connection.execute('SELECT * FROM TYPE');
|
||||||
|
console.log(result.rows); // 输出查询结果
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
} finally {
|
||||||
|
if (connection) {
|
||||||
|
// 关闭数据库连接
|
||||||
|
try {
|
||||||
|
await connection.close();
|
||||||
|
console.log('Connection closed.');
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
// app.js
|
||||||
|
const express=require("express");
|
||||||
|
const path=require("path")
|
||||||
|
// 处理跨域的插件
|
||||||
|
const cors = require('cors')
|
||||||
|
// SSE相关信息路由
|
||||||
|
const sseInfoRouter = require('./routes/sse/infoPush');
|
||||||
|
|
||||||
|
const cookieParser = require('cookie-parser')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const app= express();
|
||||||
|
|
||||||
|
const bodyParser = require('body-parser');
|
||||||
|
const multer = require('multer');
|
||||||
|
|
||||||
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
|
app.use(multer({ dest: '/tmp/'}).array('image'));
|
||||||
|
|
||||||
|
app.use(cookieParser())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 使用跨域插件
|
||||||
|
app.use(cors())
|
||||||
|
|
||||||
|
// app.use('/public', express.static('public'))
|
||||||
|
app.use(express.static('public'))
|
||||||
|
app.use(express.static('/tmp/'))
|
||||||
|
|
||||||
|
// app.use('/public', express.static('public'));
|
||||||
|
|
||||||
|
app.get('/get.html', function (req, res) {
|
||||||
|
res.sendFile( __dirname + "/" + "get.html" );
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(__dirname)
|
||||||
|
|
||||||
|
app.use('/sse', sseInfoRouter);
|
||||||
|
|
||||||
|
// 配置解析表单数据的中间件
|
||||||
|
app.use(express.urlencoded({ extended: false }))
|
||||||
|
|
||||||
|
// 导入路由模块
|
||||||
|
const apiRouter = require('./apiRouter.js')
|
||||||
|
// 把路由模块,注册到 app 上
|
||||||
|
app.use('/api', apiRouter)
|
||||||
|
|
||||||
|
//端口
|
||||||
|
app.listen(3000,function () {
|
||||||
|
console.log("127.0.0.1:3000")
|
||||||
|
});
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
const mysql = require('mysql2');
|
||||||
|
const pool = mysql.createPool({
|
||||||
|
host: 'localhost',
|
||||||
|
user: 'root',
|
||||||
|
password: 'Skyinno251,',
|
||||||
|
database: 'appserver',
|
||||||
|
connectionLimit: 10 // 连接池最大连接数
|
||||||
|
});
|
||||||
|
|
||||||
|
pool.query('SELECT * FROM user', (err, rows) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('Error querying database:', err);
|
||||||
|
//res.status(500).send('Internal Server Error');
|
||||||
|
} else {
|
||||||
|
//res.json(rows);
|
||||||
|
}
|
||||||
|
});
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"name": "sse_demo",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "sse",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"start": "nodemon --ext js,html,css index.js",
|
||||||
|
"startServer": "nodemon server.js"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"sse"
|
||||||
|
],
|
||||||
|
"author": "wenfei",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"body-parser": "^2.2.0",
|
||||||
|
"cookie-parser": "^1.4.7",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"express": "^5.1.0",
|
||||||
|
"moment": "^2.30.1",
|
||||||
|
"multer": "^1.4.5-lts.2",
|
||||||
|
"mysql2": "^3.14.1",
|
||||||
|
"nodemon": "^3.1.10"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>文件上传表单</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h3>文件上传:</h3>
|
||||||
|
选择一个文件上传: <br />
|
||||||
|
<form action="/api/file_upload" method="post" enctype="multipart/form-data">
|
||||||
|
<input type="file" name="image" size="50" />
|
||||||
|
<br />
|
||||||
|
<input type="submit" value="上传文件" />
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<form action="/api/process_get" method="GET">
|
||||||
|
First Name: <input type="text" name="first_name"> <br>
|
||||||
|
|
||||||
|
Last Name: <input type="text" name="last_name">
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<form action="/api/process_post" method="POST">
|
||||||
|
First Name: <input type="text" name="first_name"> <br>
|
||||||
|
|
||||||
|
Last Name: <input type="text" name="last_name"><br/>
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>SSE测试</title></head>
|
||||||
|
<body>
|
||||||
|
<ul id="ul"></ul>
|
||||||
|
</body>
|
||||||
|
<script>
|
||||||
|
//生成li元素
|
||||||
|
function createLi(data) {
|
||||||
|
let li = document.createElement("li");
|
||||||
|
li.innerHTML = String(data.message);
|
||||||
|
return li;
|
||||||
|
}
|
||||||
|
|
||||||
|
//判断当前浏览器是否支持SSE
|
||||||
|
let source = ''
|
||||||
|
if (!!window.EventSource) {
|
||||||
|
source = new EventSource('http://localhost:8088/sse/');
|
||||||
|
} else {
|
||||||
|
throw new Error("当前浏览器不支持SSE")
|
||||||
|
}
|
||||||
|
//对于建立链接的监听
|
||||||
|
source.onopen = function (event) {
|
||||||
|
console.log(source.readyState);
|
||||||
|
console.log("长连接打开");
|
||||||
|
};
|
||||||
|
//对服务端消息的监听
|
||||||
|
source.onmessage =
|
||||||
|
function (event) {
|
||||||
|
console.log(JSON.parse(event.data));
|
||||||
|
console.log("收到长连接信息");
|
||||||
|
let li = createLi(JSON.parse(event.data));
|
||||||
|
document.getElementById("ul").appendChild(li)
|
||||||
|
};
|
||||||
|
//对断开链接的监听
|
||||||
|
source.onerror = function (event) {
|
||||||
|
console.log(source.readyState);
|
||||||
|
console.log("长连接中断");
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Document</title></head>
|
||||||
|
<body>
|
||||||
|
<ul id="ul"></ul>
|
||||||
|
</body>
|
||||||
|
<script>
|
||||||
|
//生成li元素
|
||||||
|
function createLi(data) {
|
||||||
|
let li = document.createElement("li");
|
||||||
|
li.innerHTML = String(data.message);
|
||||||
|
return li;
|
||||||
|
}
|
||||||
|
|
||||||
|
//判断当前浏览器是否支持SSE
|
||||||
|
let source = ''
|
||||||
|
if (!!window.EventSource) {
|
||||||
|
source = new EventSource('http://localhost:8088/sse/');
|
||||||
|
} else {
|
||||||
|
throw new Error("当前浏览器不支持SSE")
|
||||||
|
}
|
||||||
|
//对于建立链接的监听
|
||||||
|
source.onopen = function (event) {
|
||||||
|
console.log(source.readyState);
|
||||||
|
console.log("长连接打开");
|
||||||
|
};
|
||||||
|
//对服务端消息的监听
|
||||||
|
source.onmessage =
|
||||||
|
function (event) {
|
||||||
|
console.log(JSON.parse(event.data));
|
||||||
|
console.log("收到长连接信息");
|
||||||
|
let li = createLi(JSON.parse(event.data));
|
||||||
|
document.getElementById("ul").appendChild(li)
|
||||||
|
};
|
||||||
|
//对断开链接的监听
|
||||||
|
source.onerror = function (event) {
|
||||||
|
console.log(source.readyState);
|
||||||
|
console.log("长连接中断");
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</html>
|
||||||
Loading…
Reference in New Issue