以下是一个基础的Node.js HTTP服务器程序,包含了MSSQL数据库的连接、GET和POST请求处理、防SQL注入、防火墙和路由功能:

const http = require('http');
const url = require('url');
const querystring = require('querystring');
const mssql = require('mssql');

// MSSQL数据库连接配置
const config = {
    user: 'username',
    password: 'password',
    server: 'localhost',
    database: 'database'
};

// 创建防火墙规则
const firewall = (req, res) => {
    // 可以在此处添加自定义规则,如IP黑名单等
    return true;
};

// 创建防SQL注入函数
const sanitize = (str) => {
    // 可以在此处添加自定义规则,如过滤特殊字符等
    return str.replace(/'/g, "''");
};

// 创建路由映射表
const routes = {
    '/': (req, res) => {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('<html><body><h1>Home Page</h1></body></html>');
        res.end();
    },
    '/login': (req, res) => {
        if (req.method === 'POST') {
            let body = '';
            req.on('data', (chunk) => {
                body += chunk.toString();
            });
            req.on('end', () => {
                const data = querystring.parse(body);
                const username = sanitize(data.username);
                const password = sanitize(data.password);
                // 执行MSSQL查询
                mssql.connect(config, (err) => {
                    if (err) {
                        console.log(err);
                        res.writeHead(500, {'Content-Type': 'text/html'});
                        res.write('<html><body><h1>500 Internal Server Error</h1></body></html>');
                        res.end();
                    } else {
                        const request = new mssql.Request();
                        request.query(`SELECT * FROM users WHERE username = '${username}' AND password = '${password}'`, (err, result) => {
                            if (err) {
                                console.log(err);
                                res.writeHead(500, {'Content-Type': 'text/html'});
                                res.write('<html><body><h1>500 Internal Server Error</h1></body></html>');
                                res.end();
                            } else if (result.recordset.length === 0) {
                                res.writeHead(401, {'Content-Type': 'text/html'});
                                res.write('<html><body><h1>401 Unauthorized</h1></body></html>');
                                res.end();
                            } else {
                                res.writeHead(200, {'Content-Type': 'text/html'});
                                res.write('<html><body><h1>Login Successful</h1></body></html>');
                                res.end();
                            }
                        });
                    }
                });
            });
        } else {
            res.writeHead(405, {'Content-Type': 'text/html'});
            res.write('<html><body><h1>405 Method Not Allowed</h1></body></html>');
            res.end();
        }
    },
    '/profile': (req, res) => {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('<html><body><h1>Profile Page</h1></body></html>');
        res.end();
    }
};

// 创建HTTP服务器
const server = http.createServer((req, res) => {
    if (firewall(req, res)) {
        const path = url.parse(req.url).pathname;
        const route = routes[path];
        if (route) {
            route(req, res);
        } else {
            res.writeHead(404, {'Content-Type': 'text/html'});
            res.write('<html><body><h1>404 Not Found</h1></body></html>');
            res.end();
        }
    }
});

// 启动HTTP服务器
server.listen(3000, () => {
    console.log('HTTP server running at http://localhost:3000/');
});

上述程序可以处理GET和POST请求,并对输入进行防SQL注入处理。同时,可以自定义防火墙规则,如IP黑名单等,以及自定义路由映射表,实现更为灵活的路由控制。

nodJS写全面httpserver程序包含mssql post get 防sql注入 防火墙 路由

原文地址: https://www.cveoy.top/t/topic/5V7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录