如何通过URL参数将数据存入MongoDB集合?
问题描述
我正在完成CSCI课程的网络安全研究项目,目标之一是模拟钓鱼网站。目前已实现通过表单将邮箱、密码数据存入MongoDB,但无法修改代码以支持通过URL参数(如https://URL.com/insert?email=EMAIL@gmail.com&password=123456)的方式存入数据。我是API开发新手,需要帮助。
以下是已编写的代码:
HTML代码
<!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> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <h1>To MongoDB</h1> <form class="container" method="post" action="/"> <div class="form-group"> <input class="form-control" name="email"> </div> <div> <textarea class="form-control" name="password"></textarea> </div> <button>ADD</button> </form> </body> </html>
Node.js代码
const express = require('express'); const app = express(); const mongoose = require('mongoose'); const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({extended: true})); mongoose.connect("mongodb+srv://<USERNAME>:<PASS>@<CLUSTER>.mongodb.net/Data", { useNewUrlParser: true}, { useUnifiedTopology: true}) const dataSchema = { email: String, password: String } const Data = mongoose.model("Data", dataSchema); app.get("/", function(req, res) { //res.send("express is working"); res.sendFile(__dirname + "/index.html"); }) //app.post app.post("/", function(req, res){ let newData = new Data({ email: req.body.email, password: req.body.password }); newData.save(); res.redirect("/"); }) app.listen(3000, function() { console.log("server is running on port : 3000"); })
解决方案
要支持URL参数存入数据,只需新增一个GET路由,通过req.query获取URL中的参数并执行保存操作。修改后的Node.js代码如下:
const express = require('express'); const app = express(); const mongoose = require('mongoose'); const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({extended: true})); // 合并MongoDB连接配置项(原代码的配置写法有误,已修正) mongoose.connect("mongodb+srv://<USERNAME>:<PASS>@<CLUSTER>.mongodb.net/Data", { useNewUrlParser: true, useUnifiedTopology: true }) const dataSchema = { email: String, password: String } const Data = mongoose.model("Data", dataSchema); app.get("/", function(req, res) { res.sendFile(__dirname + "/index.html"); }) app.post("/", function(req, res){ let newData = new Data({ email: req.body.email, password: req.body.password }); newData.save(); res.redirect("/"); }) // 新增支持URL参数的GET路由 app.get("/insert", function(req, res) { // 从URL查询参数中提取email和password const { email, password } = req.query; // 检查必要参数是否存在 if (email && password) { const newData = new Data({ email, password }); newData.save() .then(() => res.send("数据已成功存入MongoDB")) .catch(err => res.send(`存入失败:${err.message}`)); } else { res.send("缺少必要参数:请同时提供email和password"); } }) app.listen(3000, function() { console.log("server is running on port : 3000"); })
关键说明
- Express中,URL查询参数通过
req.query对象获取,比如req.query.email对应URL中的email参数 - 新增的
/insert路由可处理类似http://localhost:3000/insert?email=test@example.com&password=123456的请求 - 加入参数检查和错误捕获,避免无参数时的异常,同时方便调试问题
内容的提问来源于stack exchange,提问作者Spencer
相关产品推荐
相关产品推荐

