问题描述: 我正在使用Node.js和Express构建一个FHIR服务器,并try 通过发送POST请求来创建一个新的患者资源.然而,尽管使用不同的REST客户端(ThUnder客户端和VS Code REST客户端)来发送请求,但我遇到了服务器接收未定义的请求体的问题.
采取的步骤:
已判断请求正文格式:我验证了JSON请求正文使用必填字段正确格式化,包括"resource Type"、"name"、"Gender"和"Birth Date".
客户端配置:我确保使用适当的fhirVersion将两个REST客户端中的标头(包括"Content-Type"和"Accept"正确设置为"application/fhir+json").
请求方法:我确认我使用的是请求的POST方法.
终结点URL:我验证了终结点URL设置为http://localhost:3000/4_0_0/Patient以匹配服务器路由.
内容长度标题:内容长度标题似乎是由客户端自动设置的,我没有手动修改它.
REST客户端设置:我判断了两个REST客户端中可能影响请求正文发送方式的任何特定设置或选项.我没有发现任何可能导致该问题的设置.
重新启动服务器:我try 重新启动服务器,以防对服务器代码的更改需要生效.
try 了不同的客户端:我try 同时使用ThUnder客户端和VS Code REST客户端发送请求,以查看问题是否特定于一个客户端,但问题在两个客户端中都存在.
判断错误:我在客户端控制台或日志(log)中查找错误消息或警告,但没有找到任何深入了解问题的信息.
尽管做出了这些努力,但我仍然面临这样的问题:在try 创建患者资源时,服务器收到的请求正文未定义.
const { VERSIONS } = constants;
const express = require('express');
let config = {
profiles: {
patient: {
service: './patient.service.js',
versions: [VERSIONS['4_0_0']],
},
},
};
let server = initialize(config);
let logger = loggers.get('default');
// Create an Express application
const app = express();
// In-memory data store for Patient resources
const patientDataStore = [];
// creating a Patient resource
app.post('/4_0_0/Patient', (req, res) => {
console.log('Received POST request with data:', req.body);
console.log('Request headers:', req.headers);
const patientData = req.body;
console.log('Received POST request with data:', patientData);
// Perform validation, data storage, etc.
// For simplicity, we'll just push the patientData to the in-memory store
patientDataStore.push(patientData);
console.log('Patient data stored:', patientDataStore);
// Send a response indicating success (HTTP status code 201 for resource creation)
res.status(201).json({ message: 'Patient resource created successfully' });
});
// Define route handlers for GET requests to the URLs
app.get('/4_0_0/', (req, res) => {
res.send('GET request to /4_0_0/');
});
app.get('/4_0_0/Patient', (req, res) => {
console.log('Data in patientDataStore:', patientDataStore);
res.json(patientDataStore);
});
// Define a route to retrieve and display patient data
app.get('/patientData', (req, res) => {
console.log('GET request to /patientData');
console.log('Data in patientDataStore:', patientDataStore);
res.json(patientDataStore);
});
const fhirMiddleware = (req, res, next) => {
server(req, res, next);
};
app.use('/fhir', fhirMiddleware);
const PORT = 3000;
app.listen(PORT, () => {
logger.info(`Starting the FHIR Server at localhost:${PORT}`);
});```