https://198.162.31.177:3000/app/all 访问不到const express = require('express');
const app = express();
const PORT = 3000;
const https = require('https');
const fs = require('fs');
const os = require('os');
// 获取本地IP地址函数
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const ifaceName in interfaces) {
const iface = interfaces[ifaceName];
for (const config of iface) {
if (config.family === 'IPv4' && !config.internal) {
return config.address;
}
}
}
return '127.0.0.1';
}
// 获取公网IP地址函数
function getPublicIP() {
return new Promise((resolve, reject) => {
https.get('https://api.ipify.org?format=json', (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const result = JSON.parse(data);
resolve(result.ip || 'your-external-ip');
} catch (e) {
resolve('your-external-ip');
}
});
}).on('error', (err) => {
console.error('获取公网IP失败:', err.message);
resolve('your-external-ip');
});
});
}
// 模拟数据库的简化的产品数据服务
const productService = {
getAllProducts: () => ({
"status": "200",
"text": "success",
"data": {
"bancais": [
{
"houdu": 15,
"mupi1": {
"id": 1
},
"mupi2": {
"id": 1
},
"caizhi": {
"id": 1
},
"kucun": {
"id": 1
},
"id": 1
},
{
"houdu": 15,
"mupi1": {
"id": 2
},
"mupi2": {
"id": 2
},
"caizhi": {
"id": 1
},
"kucun": {
"id": 2
},
"id": 2
}
],
"dingdans": [
{
"dingdanChanpinZujian": [
],
"number": "直接入库",
"id": 1,
"dingdanChanpin": [
{"id":1}
]
}
],
"mupis": [
{
"name": "千和板",
"id": 1,
"you": null
},
{
"name": "桃花芯",
"id": 2,
"you": null
},
{
"name": "杉木",
"id": 3,
"you": null
}
],
"chanpins": [
{
"chanpinZujian": [
{
"id": 1
}
],
"bianhao": "直接入库",
"id": 1,
"dingdanChanpin": [
{"id":1}
]
}
],
"kucuns": [
{
"bancai": {
"Id": 1
},
"shuliang": 12,
"id": 1
},
{
"bancai": {
"Id": 2
},
"shuliang": 32,
"id": 2
}
],
"chanpin_zujians": [
{
"chanpin": {
"Id": 1
},
"bancai": {
"id": 1
},
"id": 1,
"zujian": {
"Id": 1
},
"one_howmany": 15
}
],
"chanpin_zujians": [
{
"chanpin": {
"Id": 1
},
"bancai": {
"id": 1
},
"id": 1,
"zujian": {
"Id": 1
},
"one_howmany": 15
}
],
"zujians": [
{
"chanpinZujian": [
{
"id": 1
}
],
"name": "前板",
"id": 1
}
],
"caizhis": [
{
"name": "千和板",
"bancai": [
{
"id": 1
},
{
"id": 2
}
],
"id": 1
},
{
"name": "杉木",
"bancai": [
],
"id": 3
},
{
"name": "桃花芯",
"bancai": [
],
"id": 2
}
],
"users": [
{
"role": 0,
"name": "超管",
"id": 1,
"andy": "123456"
}
]
}
}),
addBancai: (data) => {
console.log("🚀 添加新板材:", data);
return {
id: Math.floor(Math.random() * 100) + 1,
...data,
timestamp: new Date().toISOString()
};
}
};
// 添加中间件:记录所有请求
app.use((req, res, next) => {
console.log('📥 收到请求:', {
method: req.method,
url: req.url,
ip: req.ip || req.connection.remoteAddress,
timestamp: new Date().toISOString()
});
next();
});
// 添加JSON请求体解析中间件
app.use(express.json());
// GET路由:获取所有产品
app.get('/app/all', (req, res) => {
console.log('🔍 正在处理产品查询请求');
try {
const products = productService.getAllProducts();
console.log('📊 产品数据已获取:', JSON.stringify(products, null, 2));
res.json({
status: 200,
text: "success",
data: products
});
} catch (error) {
console.error('❌ 产品查询错误:', error.message);
res.status(500).json({
status: 500,
text: "服务器错误",
error: error.message
});
}
});
// POST路由:添加新板材
app.post('/app/add/bancai', (req, res) => {
console.log('📝 添加板材请求体:', JSON.stringify(req.body, null, 2));
try {
const newBancai = productService.addBancai(req.body);
console.log('✅ 板材添加成功:', newBancai);
res.json({
status: 200,
text: "success",
data: newBancai
});
} catch (error) {
console.error('❌ 添加板材错误:', error.message);
res.status(500).json({
status: 500,
text: "服务器错误",
error: error.message
});
}
});
// 启动服务器
async function startServer() {
try {
const publicIP = await getPublicIP();
const localIP = getLocalIP();
app.listen(PORT, '0.0.0.0', () => {
console.log('\n');
console.log('🚀 =================== 服务器已启动 ===================');
console.log('📡 访问地址:');
console.log(` → 本地访问: http://localhost:${PORT}/app/all`);
console.log(` → 局域网访问: http://${localIP}:${PORT}/app/all`);
console.log(` → 外网访问: http://${publicIP}:${PORT}/app/all (需端口转发)`);
console.log('');
console.log('🔗 示例请求:');
console.log(` GET请求: curl http://localhost:${PORT}/app/all`);
console.log(` POST请求: curl -X POST http://localhost:${PORT}/app/add/bancai -H "Content-Type: application/json" -d '{"houdu": 18, "material": "wood"}'`);
console.log('====================================================');
console.log('\n');
});
} catch (error) {
console.error('❌ 启动服务器失败:', error);
}
}
// 开始运行服务器
startServer();
🚀 =================== 服务器已启动 ===================
📡 访问地址:
→ 本地访问: http://localhost:3000/app/all
→ 局域网访问: http://192.168.31.177:3000/app/all
→ 外网访问: http://your-external-ip:3000/app/all (需端口转发)
🔗 示例请求:
GET请求: curl http://localhost:3000/app/all
POST请求: curl -X POST http://localhost:3000/app/add/bancai -H "Content-Type: application/json" -d '{"houdu": 18, "material": "wood"}'
最新发布