Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

04 request body and cors #1

Open
wants to merge 4 commits into
base: 01-basic-setup
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Creating a RESTful API with Node.js
This repository accompanies my YouTube video series on building a RESTful API with Node.js
This repository accompanies my YouTube video series on building a RESTful API with Node.js: [https://github.com/academind/node-restful-api-tutorial.git](https://github.com/academind/node-restful-api-tutorial.git)

## Usage
Check out the branch you're interested in (i.e. which belongs to the video in my series you just watched), ```git clone``` it and thereafter run ```npm install```.
36 changes: 36 additions & 0 deletions api/routes/orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const express = require('express');
const router = express.Router();

// Handle incoming GET requests to /orders
router.get('/', (req, res, next) => {
res.status(200).json({
message: 'Orders were fetched'
});
});

router.post('/', (req, res, next) => {
const order = {
productId: req.body.productId,
quantity: req.body.quantity
};
res.status(201).json({
message: 'Order was created',
order: order
});
});

router.get('/:orderId', (req, res, next) => {
res.status(200).json({
message: 'Order details',
orderId: req.params.orderId
});
});

router.delete('/:orderId', (req, res, next) => {
res.status(200).json({
message: 'Order deleted',
orderId: req.params.orderId
});
});

module.exports = router;
47 changes: 47 additions & 0 deletions api/routes/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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) => {
const product = {
name: req.body.name,
price: req.body.price
};
res.status(201).json({
message: 'Handling POST requests to /products',
createdProduct: product
});
});

router.get('/:productId', (req, res, next) => {
const id = req.params.productId;
if (id === 'special') {
res.status(200).json({
message: 'You discovered the special ID',
id: id
});
} else {
res.status(200).json({
message: 'You passed an ID'
});
}
});

router.patch('/:productId', (req, res, next) => {
res.status(200).json({
message: 'Updated product!'
});
});

router.delete('/:productId', (req, res, next) => {
res.status(200).json({
message: 'Deleted product!'
});
});

module.exports = router;
45 changes: 40 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
const express = require('express');
const express = require("express");
const app = express();
const morgan = require("morgan");
const bodyParser = require("body-parser");

const productRoutes = require("./api/routes/products");
const orderRoutes = require("./api/routes/orders");

app.use(morgan("dev"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET');
return res.status(200).json({});
}
next();
});

// Routes which should handle requests
app.use("/products", productRoutes);
app.use("/orders", orderRoutes);

app.use((req, res, next) => {
res.status(200).json({
message: 'It works!'
});
const error = new Error("Not found");
error.status = 404;
next(error);
});

app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
});
});

module.exports = app;
module.exports = app;
Loading