如何在Node.js环境中调用Europe PMC SOAP Web服务?求JavaScript示例
Hey folks! If you're looking to interact with the Europe PMC SOAP Web Service using Node.js, I've got you covered. Let's go through the process step by step, including a fully functional example to get you up and running quickly.
1. First, Install the Required Dependency
Node.js doesn't have built-in SOAP support, so we'll use the popular soap npm package to handle the heavy lifting. Install it via npm:
npm install soap
2. Know the Europe PMC SOAP WSDL
Europe PMC provides a public WSDL for its SOAP services, which acts as the "blueprint" for the service methods and data structures. The main WSDL URL is:https://www.ebi.ac.uk/europepmc/webservices/soap?wsdl
We'll use this URL to generate a SOAP client in our code.
3. Core Steps to Make the Call
Here's the high-level workflow for interacting with the service:
- Import the
soaplibrary - Generate a SOAP client by loading the WSDL
- Call the specific service method with your desired parameters
- Handle the response (or any errors that pop up)
4. Full Working Example
Let's put this into action with an example that uses the searchPMC method to fetch papers related to the keyword "CRISPR". This method returns a list of matching publications along with metadata:
const soap = require('soap'); // WSDL URL for Europe PMC SOAP service const europePMCWsdl = 'https://www.ebi.ac.uk/europepmc/webservices/soap?wsdl'; // Async function to handle the SOAP call async function fetchEuropePMCPapers() { try { // Create the SOAP client (async version for cleaner code) const client = await soap.createClientAsync(europePMCWsdl); // Define your request parameters // Check Europe PMC's docs for all available params per method const requestOptions = { query: 'CRISPR', pageSize: 5, // Return 5 results page: 1, // Fetch the first page sort: 'CITED' // Sort by citation count (optional) }; // Call the searchPMC method (async variant) const [response] = await client.searchPMCAsync(requestOptions); // Process the results console.log(`Total papers found: ${response.hitCount}`); console.log('\nFirst 3 papers:'); response.resultList.result.slice(0, 3).forEach((paper, index) => { console.log(`\n${index + 1}. ${paper.title}`); console.log(` Authors: ${paper.authorString}`); console.log(` Journal: ${paper.journalTitle}`); }); } catch (err) { console.error('Error calling Europe PMC SOAP service:', err); } } // Run the function fetchEuropePMCPapers();
5. Key Notes to Keep in Mind
- Async/Await: We're using the
asyncvariants of thesoapmethods (likecreateClientAsyncandsearchPMCAsync) to avoid callback hell and make the code more readable. - Parameter Validation: Make sure to pass the correct parameters for each method. You can refer to Europe PMC's SOAP service documentation to see all available methods and their required/optional parameters.
- Client Configuration: If you need to adjust timeouts, add headers, or tweak other settings, you can pass a configuration object when creating the client:
const client = await soap.createClientAsync(europePMCWsdl, { timeout: 15000, // 15-second timeout // Add other configs like proxy, headers, etc. here });
内容的提问来源于stack exchange,提问作者Yuci

