Advertisement
Guest User

server

a guest
Nov 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var port = 3000;
  2.  
  3. var today = require('./today');
  4. var express = require('express');
  5. var app = express();
  6. var request = require('request');
  7.  
  8. app.get('/api/today', function(req, res) {
  9.     var body = "The day of the week is " + today() + ".";
  10.     res.type('text/plain');
  11.     res.set('Content-Length', Buffer.byteLength(body));
  12.     res.status(200).send(body);
  13. });
  14.  
  15. app.get('/api/weather', function(req, res) {
  16.     var options = {
  17.         method: 'GET',
  18.         uri: 'http://weather.gov/xml/current_obs/KSFO.xml',
  19.         headers: {
  20.             'User-agent': 'weatherRequest/1.0'
  21.         }
  22.     };
  23.     var callback = function(error,response,body) {
  24.         if (error) {
  25.             res.status(500).send(error.message);
  26.         }
  27.         res.type('text/plain');
  28.         res.status(response.statusCode).send(body);
  29.     };
  30.     request(options, callback);
  31. });
  32.  
  33. app.listen(port, function() {
  34.     console.log('Listening on port %s.', port);
  35. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement