You can add additional endpoints for now it has 2:
- GET '/' - Welcome message
- GET '/proxy?url=...' - makes a request to specified URL and pipes it back to the client. Mostly used for avoiding CORS issues.
To run locally:
- Run 'npm i'
- If you want linter to work install VSCode extension TSLint
- Run 'npm run dev'
- The API should be available at 'localhost:3000'
Example usage in browser
fetch(`http://localhost:3000/proxy?url=https://api.meteo.lt/v1/places`)
.then(async res => {
console.log(await res.json())
})
// some URLs that contain certain characters found
// @ https://www.werockyourweb.com/url-escape-characters/
//, must be escaped as shown below:
fetch(`http://localhost:3000/proxy?url=${
encodeURI('https://api.meteo.lt/v1/places')
}`)
.then(async res => {
console.log(await res.json())
})
- Deploy App to hosting service. Example Deployment to Heroku described below
Example usage of Live API (According to Heroku deployment example)
fetch(`https://test-534.herokuapp.com/proxy?url=https://api.meteo.lt/v1/places`)
.then(async res => {
console.log(await res.json())
})
- Run 'npm run build' which will create 'dist' directory. This will compile TypeScript to JavaScript.
- In Heroku create new App
- Set App to Deploy from Github
- For initial deploy click 'Deploy Branch' (master for example) and then 'Enable Automatic Deploys'
- Go to logs and you should see 'API is running on port ...' when API is ready and can accept requests
- You can also check if API is live if you go to 'Open App' it will make a GET requests to endpoint '/' which should return a 'welcome' message.