如何为已搭建的Asymmetrik/node-fhir-server-mongo启用POST操作
Hey there, I’ve messed around with this FHIR server setup before, so I can walk you through getting POST operations up and running—here’s what you need to do:
1. Update Resource Configuration to Allow Create Operations
First, head to your server’s core config file (usually src/config.js or the config block in server.js). This is where you define which operations are enabled for each FHIR resource.
By default, the operations array for each resource probably only includes read, search, and update (mapping to GET and PUT actions). You need to add create to this list to enable POST. For example:
// Example config snippet module.exports = { server: { // ... other server configs }, resources: { patient: { operations: ['read', 'search', 'update', 'create'], // Add 'create' here service: require('./services/patient/patient.service') }, observation: { operations: ['read', 'search', 'update', 'create'], // Repeat for other resources you need service: require('./services/observation/observation.service') } // ... other FHIR resources } };
2. Implement the Create Method in Resource Services
Next, make sure the corresponding resource service file (like patient.service.js) has a create method implemented. The default service files might already have a skeleton for this, but you’ll need to flesh out the logic to save the resource to MongoDB.
Here’s a basic example of what the create method might look like:
// In patient.service.js const PatientModel = require('../models/patient.model'); module.exports.create = async (args) => { // Extract the FHIR resource from the request arguments const { resource } = args; // Save the resource to your MongoDB collection const createdPatient = await PatientModel.create(resource); // Convert the MongoDB document to a FHIR-compliant response (map _id to FHIR id) return { ...createdPatient.toObject(), id: createdPatient._id.toString(), resourceType: 'Patient' }; };
- Double-check that your MongoDB model (like
patient.model.js) is set up correctly with the right schema for the FHIR resource. - If you’re using the default models provided by the repo, they should already handle FHIR data structures, so you just need to wire up the save logic.
3. Restart Your Server & Test the POST Endpoint
Once you’ve updated the config and service files, restart your FHIR server. You can now test the POST operation using tools like Postman or curl:
# Example curl command to create a Patient resource curl -X POST \ http://localhost:3000/fhir/Patient \ -H "Content-Type: application/fhir+json" \ -d '{ "resourceType": "Patient", "name": [{"family": "Doe", "given": ["John"]}], "gender": "male", "birthDate": "1990-01-01" }'
Bonus Notes
- If you need batch/transaction support, you can add
create-batchorcreate-transactionto theoperationsarray for your resources. - Make sure your server has proper error handling for invalid FHIR resources—you might want to add validation logic in the
createmethod to ensure the incoming resource adheres to FHIR specs.
内容的提问来源于stack exchange,提问作者san

