Home > Article > Web Front-end > How to use Node.js to develop a shopping cart function for an online mall
How to use Node.js to develop the shopping cart function of an online mall
In today's Internet era, e-commerce has become one of the main ways for people to shop. A complete shopping cart function is very important for online shopping malls. It can provide users with a convenient shopping experience and improve user conversion rates. This article will introduce how to use Node.js to develop a shopping cart function for an online mall and provide specific code examples.
mkdir online-store cd online-store npm init -y
These The command will create a folder named online-store and generate a package.json file in it to record the project's dependencies and other related information.
npm install express express-session body-parser ejs --save
These dependency packages include the Express framework, Express- Session, Body Parser and EJS template engine.
const express = require('express'); const session = require('express-session'); const bodyParser = require('body-parser'); const app = express(); app.set('view engine', 'ejs'); app.use(express.static('public')); app.use(bodyParser.urlencoded({ extended: false })); app.use(session({ secret: 'my-secret-key', resave: false, saveUninitialized: true })); app.listen(3000, () => { console.log('Server is running on port 3000'); });
This code uses Express The framework creates a simple server and sets up the EJS template engine and a directory for static files.
app.get('/', (req, res) => { res.render('index', { message: req.session.message }); }); app.post('/add-to-cart', (req, res) => { // 处理添加商品到购物车的逻辑 }); app.get('/cart', (req, res) => { // 显示购物车页面 }); app.get('/checkout', (req, res) => { // 结算购物车中的商品 }); app.get('/success', (req, res) => { req.session.message = '订单支付成功!'; res.redirect('/'); });
This code defines four routes, They are used to display the home page, process the logic of adding products to the shopping cart, display the shopping cart page, and check out the products in the shopping cart.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Online Store</title> </head> <body> <h1>Welcome to Online Store!</h1> <% if (message) { %> <p><%= message %></p> <% } %> <form action="/add-to-cart" method="post"> <input type="hidden" name="product" value="Product A"> <button type="submit">Add to Cart</button> </form> <a href="/cart">View Cart</a> <a href="/checkout">Checkout</a> </body> </html>
This view template is used to display the homepage and provides links to add items to the shopping cart, view the shopping cart, and check out the shopping cart.
app.post('/add-to-cart', (req, res) => { const product = req.body.product; req.session.cart = req.session.cart || []; req.session.cart.push(product); res.redirect('/'); }); app.get('/cart', (req, res) => { const cart = req.session.cart || []; res.render('cart', { cart }); }); app.get('/checkout', (req, res) => { const cart = req.session.cart || []; req.session.cart = []; res.render('checkout', { cart }); });
This code adds products through requests Go to the shopping cart and display the items in the shopping cart on the shopping cart page and checkout page.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Shopping Cart</title> </head> <body> <h1>Your Shopping Cart</h1> <% if (cart.length > 0) { %> <ul> <% cart.forEach(product => { %> <li><%= product %></li> <% }) %> </ul> <% } else { %> <p>Your shopping cart is empty.</p> <% } %> <a href="/checkout">Checkout</a> </body> </html>
This view template uses Displays the list of products in the shopping cart and provides a link to the checkout shopping cart.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Checkout</title> </head> <body> <h1>Checkout</h1> <% if (cart.length > 0) { %> <ul> <% cart.forEach(product => { %> <li><%= product %></li> <% }) %> </ul> <p>Thank you for your order!</p> <% } else { %> <p>Your shopping cart is empty.</p> <% } %> <a href="/success">Pay Now</a> </body> </html>
This view template is used Displays the page after the checkout cart and provides a payment link.
node app.js
Then visit http:// in the browser /localhost:3000 , you will see a simple online mall page. You can click the "Add to Cart" button to add items to the shopping cart and view the items in the shopping cart on the shopping cart page and checkout page.
Summary
This article introduces how to use Node.js to develop a shopping cart function for an online mall. By using the Express framework, we can quickly build a simple server and use the EJS template engine to render the view. The shopping cart function is implemented using Express-Session, and node sessions are used to store shopping cart data. I hope this article will help you understand how to use Node.js to develop the shopping cart function of an online mall.
The above is the detailed content of How to use Node.js to develop a shopping cart function for an online mall. For more information, please follow other related articles on the PHP Chinese website!