See Related Information below for other tutorials for creating APIs.
This tutorial is based on Maximillian Schwarzmüller's Creating a REST API with Node.js starting with Part 2:
const http = require('http'); const express = require('express'); const app = express(); const port = process.env.PORT || 3000; const server = http.createServer(app); server.listen(port); app.use((req, res, next) => { res.status(200).json({ message: 'It works!' }) }); module.exports = app;
const express = require('express'); const app = express(); const productRoutes = require('./api/routes/products'); module.exports = app;
Note: Max makes changes to these two files in the next video. They work here for localhost:3000 (see max_api1.zip)
Postman provides an easy way to test various API requests.
To continue:
const express = require('express'); const router = express.Router(); router.get('/', (req, res, next) => { res.status(200).json({ message: 'Handling GET requests to /products' }); }); router.post('/', (req, res, next) => { res.status(200).json({ message: 'Handling POST requests to /products' }); }); router.get('/:productID', (req, res, next) => { const id = req.params.productId; if (id === 'special') { res.status(200).json({ message: 'You found the spcial ID', id: id }); } else { res.status(200).json({ message: 'You passed an ID' }); }; }); module.exports = router;
const express = require('express'); const app = express(); const productRoutes = require('./api/routes/products'); const orderRoutes = require('./api/routes/orders'); app.use('/products', productRoutes); app.use('/orders', orderRoutes); module.exports = app;
Select GET with localhost:3000/orders then Send; should see Orders were fetched
Select POST with localhost:3000/orders then Send; should see Order was created
Select GET with localhost:3000/orderssomeorderid then Send; should see Order details . . . orderId: someorder id
Select DELETE with localhost:3000/orders then Send; should see Order deleted
It's a drag having to restart the server with that node server.js command all the time. A package called nodemon does the restart automatically every time to change something in the files.
Notice that every time you make any change to any of your API project files, another one of the lines [nodemon] starting `node server.js` is added to the terminal window.
You'll want to see detailed error and status messages for this API project. These will appear in the terminal if you run an IDE like Visual Studio Code
Build your first Hello Mule application. Simple API, mulesoft
Create A Paginated API With Node.js - Complete Tutorial promo?
How to Create a Sports Betting Application with Python (using TheRundown API)
REST API Crash Course - Introduction + Full Python API Tutorial
The REST API Handbook – How to Build, Test, Consume, and Document REST APIs
Use Django REST Framework to Create Web APIs
Create An API: CORS, SPA / Client-Side Routing Explanation
Use React and APIs to Build a Weather App fcc
How to build a REST API with Node js & Express
Create an Industry Level REST API Using .NET fcc
An awesome guide on how to build RESTful APIs with ASP.NET Core
Tutorial: Create a minimal API with ASP.NET Core ms
Information and programs provided by admin@mcmassociates.io.