server.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import express from 'express';
  7. import fetch from 'node-fetch';
  8. import { createMapping } from './mapping';
  9. async function getMappings(id: string) {
  10. const data = await fetch(`https://www.ebi.ac.uk/pdbe/api/mappings/${id}`);
  11. const json = await data.json();
  12. return createMapping(json);
  13. };
  14. const PORT = process.env.port || 1338;
  15. const app = express();
  16. const PREFIX = '/';
  17. app.get(`${PREFIX}/:id`, async (req, res) => {
  18. try {
  19. console.log('Requesting ' + req.params.id);
  20. const mapping = await getMappings(req.params.id);
  21. res.writeHead(200, {
  22. 'Content-Type': 'text/plain; charset=utf-8',
  23. 'Access-Control-Allow-Origin': '*',
  24. 'Access-Control-Allow-Headers': 'X-Requested-With'
  25. });
  26. res.end(mapping);
  27. } catch {
  28. console.log('Failed ' + req.params.id);
  29. res.writeHead(404, { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'X-Requested-With' });
  30. res.end();
  31. }
  32. });
  33. app.get(`${PREFIX}`, (req, res) => {
  34. res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  35. res.end('Usage: /pdb_id, e.g. /1tqn');
  36. });
  37. app.listen(PORT);
  38. console.log('Running on port ' + PORT);