GithubHelp home page GithubHelp logo

ahmadad62 / nodejs-shopping-app-api-backend- Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 224 KB

I have already deployed this Back end Application on render.com.

Home Page: https://mern-stack-shopping-app-api.onrender.com/api/products

JavaScript 100.00%
express mogoose mongodb nodejs

nodejs-shopping-app-api-backend-'s Introduction

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.

nodejs-shopping-app-api-backend-'s People

Contributors

ahmadad62 avatar snyk-bot avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.