Skip to content

ahmadad62/NodeJS-Shopping-App-api-Backend-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Server

This is a Node.js server setup using Express and Mongoose to connect to a MongoDB database. The server has routes for handling user authentication, product management, cart management, order management, and Stripe checkout.

Here's a breakdown of the code:

const express = require('express'); 
const mongoose = require('mongoose'); 
const dotenv = require('dotenv'); 
const userRoute = require('./routes/user'); 
const authRoute = require('./routes/auth'); 
const productRoute = require('./routes/product'); 
const cartRoute = require('./routes/cart'); 
const orderRoute = require('./routes/order'); 
const stripeRoute = require("./routes/stripe"); 
const cors = require('cors')  const app = express(); 
dotenv.config()

Here, I import the necessary packages and set up an instance of the Express app.

async function startServer() {
    try {
        await mongoose.connect(process.env.MONGO_URL)
            .then(() => {
                console.log('connected MongoDB successfully')
            })
            .catch(err => { console.log('error connecting MongoDB', err) })

        app.listen(process.env.PORT || 5000, () => {
            console.log('Backend server is running on port 5000');
        })

    } catch (err) {
        console.log(err);
    }
}

startServer().catch(err => console.log(err));

This function connects to the MongoDB database and starts the Express server on the specified port. If there is an error, it will be caught and logged.

app.use(cors()) 
app.use(express.json()) 
app.use('/api/auth', authRoute) 
app.use('/api/users', userRoute) 
app.use('/api/products', productRoute) 
app.use('/api/carts', cartRoute) 
app.use('/api/orders', orderRoute) 
app.use("/api/checkout", stripeRoute)

Here, we enable CORS (Cross-Origin Resource Sharing) to allow cross-domain requests, parse incoming JSON data, and set up the routes for handling user authentication, product management, cart management, order management, and Stripe checkout.

To document this backend, you can provide comments for each section of the code, explaining what it does and why it's necessary. Additionally, you can use a tool like Swagger or Postman to generate API documentation, which can be useful for developers who will be using your backend to build frontend applications.

How to integrate Stripe into your React project.

Stripe is a popular payment processing platform that allows you to accept payments online. Here are the basic steps for integrating Stripe into your React project:

  1. Create a Stripe account and obtain API keys: In order to use Stripe, you'll need to create an account with them and obtain API keys. You can do this by logging in to your Stripe account and navigating to the API keys section. You'll need both the Publishable Key and the Secret Key.

  2. Install the Stripe library: You can install the Stripe library using npm by running the following command in your project directory: npm install stripe.

  3. Import the Stripe library: Once you've installed the Stripe library, you can import it into your React component using the following code: import Stripe from 'stripe'.

  4. Create a Stripe instance: In order to use the Stripe library, you'll need to create an instance of it using your Secret Key. You can do this using the following code: const stripe = new Stripe('YOUR_SECRET_KEY').

  5. Create a payment form: In order to accept payments, you'll need to create a payment form that collects the necessary information from the user, such as their name, email, and credit card details. You can use HTML and CSS to create the form, and use the Stripe library to handle the payment processing.

  6. Handle the payment: Once the user submits the payment form, you'll need to handle the payment using the Stripe library. You can do this by calling the stripe.paymentIntents.create method, passing in the necessary parameters such as the amount to charge and the currency to use. You'll also need to provide a callback function that handles the response from Stripe, such as displaying a success message if the payment was successful, or an error message if the payment failed.

That's a brief overview of how to integrate Stripe into your React project. For more information and detailed instructions, you can check out the official Stripe documentation.

Releases

No releases published

Packages

No packages published