问题描述: 我正在使用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}`);
});```

推荐答案

添加我的 comments 作为答案,这样这个问题就可以结束了.

"应用程序/FHIR+json"

您能try (在客户机上)只使用"app/json"吗?

我不确定fhir+json这个数字是否"被理解"了.

你试过仅仅发布一个简单的json吗?

{ "hello": "world" } 

go 你的新休息服务吗?

如果这有帮助,那么请标记"作为答案",这样这个问题就不会出现在"未回答的问题"上.

另外,请将您发现的问题/答案作为问题/答案附加到您的原始问题中.这样,将来的人们也可以从这个问题/答案中学习.

Node.js相关问答推荐

Mongoose更新在^8.0.3版中,许多似乎不能按预期工作

如何更改ejs中的镜像src,以防从OMDB API获取的某些镜像具有src=N/A

如何在Firebase Cloud Function v2计划函数中设置代码中的时区?

如何使用Nextjs路由从下一步/导航在新选项卡中通过";router.ush";打开链接

一个函数中的两个依赖的NodeJS数据库操作.如果第二个失败了怎么办?

如果我在 tsx 文件中使用了use client,ssr 会如何发生?

使用 NPM 8 安装本地构建

如何从mongoose 对象内嵌套的数组中提取数组元素?

node_modules/preact/src/jsx.d.ts:2145:22 - 错误 TS2304:找不到名称SVGSetElement

如何在没有 Typescript 的情况下以交互方式使用 Create-React-App?

node Axios 创建全局令牌变量以在单独的变量头中使用

一个非常奇怪的JavaScript heap out of memory问题

来自 child_process.exec 的错误没有这样的设备或地址,管道有帮助.为什么?

cURL 和 shell 任务

如果我使用像 express 这样的 node 服务器,是否需要 webpack-dev-server

node.js 是否支持yields ?

如何在 node 调试器中禁用第一行中断

如何从 Redis 保存和检索会话

如何在客户端使用 node.js 模块系统

Forever + Nodemon 一起运行