triston-notes/Cards/dev/Axios Server Boilerplate.md

27 lines
907 B
Markdown
Raw Normal View History

2023-10-21 23:52:54 +00:00
up:: [[Boilerplate Code]]
tags:: #boilerplate
## Here is an example of a simple server using Axios to handle HTTP requests:
```tsx
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.