You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
//16.apiRouter.js
const express = require ( 'express' )
const router = express . Router ( )
const oracledb = require ( 'oracledb' ) ;
const connectionString = "system/452131wW@192.168.3.29:1521/orcl" ; // 替换为实际的用户名、密码、主机名、端口号和数据库名
// 设置数据库连接参数
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 ) ;
}
}
}
}
// 在这里挂载对应的路由
router . get ( '/get' , ( req , res ) => {
// 通过 req.query 获取客户端发送到服务器的数据(查询字符串)
const query = req . query
run ( ) . then ( r => { console . log ( "调用oracle" ) } )
// 调用 res.send() 方法,向客户端响应处理的结果
res . send ( {
status : 0 , // 0 表示处理成功, 1 表示处理失败
msg : 'GRT请求成功' , // 状态的描述
data : query // 需要响应给客户端的数据
} )
} )
router . post ( '/post' , ( req , res ) => {
// 通过 req.body 获取请求体中包含的 url-encoded 格式的数据
const body = req . body
res . send ( {
status : 1 ,
msg : 'POST请求成功' ,
data : body
} )
} )
router . delete ( '/delete' , ( req , res ) => {
res . send ( {
status : 0 ,
msg : 'DELETE请求成功' ,
} )
} )
module . exports = router