如何解决如何使用 node.js 和 json 进行验证检查?
我刚开始从事网络开发,我知道它们是类似的问题,但不幸的是我找不到解决方案...
在我的 html 文件中,我有一个输入字段,用户应在其中输入其 ID。我想通过 json 文件(存储 ID 的位置)检查 ID 是否输入正确。 对我来说,我不清楚如何通过节点 js 进行此验证检查。有什么建议吗?
test.html
<form class="test">
<label class="ID-Field">
<input id="value" type="text" required/>
<span class="placeholder">Your ID</span>
</label>
</form>
data.json
{
"inforamtion":[
{
"ID": "123","Name": "Vincent"
},{
"ID": "452","Name": "Jean"
},{
"ID": "678","Name": "Sopfie"
}
]
}
index.js
const http =require('http');
const express = require('express');
const { urlencoded } = require('body-parser');
const { fstat } = require('fs');
const { response } = require('express');
const app = express();
app.get('/test.html',function (req,res) {
res.sendFile(path.join(__dirname +'/test.html'));
})
app.listen(4000,console.log(`Server running at http://localhost:4000`));
//Check ID
app.post('/database.json',(req,res) => {
const result = information.find(({id}) => id === 'defined');
console.log(result)
})
解决方法
您可以在您的节点应用程序中创建新端点,例如。
app.post('/check',(req,res) => {})
填写表单后,您可以向此端点执行 ajax 请求,您可以在该端点发送用户提供的 ID。在该端点中,您可以获取您的 json 文件,并尝试查找您是否有任何 ID 为用户私有的对象。
但首先您需要将数据 json 更改为数组才能使用 .find 函数 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
[
{ id: 1,name: "Some name"},{ id: 2,name: "Another name"}
]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。