triston-notes/Cards/dev/Axios Server Boilerplate.md
2023-10-21 18:52:54 -05:00

907 B

up:: Boilerplate Code tags:: #boilerplate

Here is an example of a simple server using Axios to handle HTTP requests:

const axios = require('axios');
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  axios.get('http://www.example.com/')
    .then((response) => {
      res.send(response.data);
    })
    .catch((error) => {
      res.send(error);
    });
});

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

In this example, the server uses the express library to create an HTTP server that listens on port 3000. When a GET request is received on the / route, the server uses Axios to send a GET request to http://www.example.com/. When the response is received, the server sends the response data back to the client. In case of an error, the error is sent back to the client instead.