Today we will write our first API and make a call to it.
In the server.js file create an HTTP server, there is already a function to read a file.
- When it receives a request with url
/
or/index.html
send the fileindex.html
as the response. - When it receives a request with
/app.js
send the fileapp.js
as the response - When it receives a request with
/api
send the filedata.json
as a the response. - When it receives a request with any other URL send message back to the user with statusCode 404.
In the app.js
- Make an AJAX get request using jQuery to the url
localhost:8000/data.json
- console.log the result
- Open
localhost:8000
in the browser with the url you want to test. - For the bonus: open up the console and see the result!