39647-vm/backend/server.js
tornikegerantia bc95274472 Add compatibility for Replit environment and local development
Update backend server to handle Webflow GraphQL endpoints and CSRF tokens, serve static files correctly, and use 0.0.0.0 host. Add a compatibility script for frontend Webflow e-commerce issues and update HTML files to include it.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 375ec6d3-d5af-4f82-ab81-5c60fd4a86a3
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 05dda85a-ad24-46c8-b27e-50c860b4dd57
Replit-Helium-Checkpoint-Created: true
2026-04-14 21:14:11 +00:00

98 lines
2.5 KiB
JavaScript

const express = require('express');
const cors = require('cors');
const path = require('path');
const crypto = require('crypto');
require('dotenv').config();
const app = express();
app.set('trust proxy', true);
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post('/.wf_graphql/csrf', (req, res) => {
const token = crypto.randomBytes(16).toString('hex');
res.cookie('wf-csrf', token, {
httpOnly: false,
sameSite: 'lax',
secure: req.secure,
});
res.status(204).send();
});
app.post(['/.wf_graphql/apollo', '/.wf_graphql/usys/apollo'], (req, res) => {
res.json({
data: {
database: {
id: 'local',
commerceOrder: {
comment: null,
extraItems: [],
id: 'local-cart',
startedOn: null,
statusFlags: {
hasDownloads: false,
hasSubscription: false,
isFreeOrder: false,
requiresShipping: false,
},
subtotal: { decimalValue: '0', string: '$0.00', unit: 'USD', value: 0 },
total: { decimalValue: '0', string: '$0.00', unit: 'USD', value: 0 },
updatedOn: null,
userItems: [],
userItemsCount: 0,
},
},
site: {
commerce: {
id: 'local-commerce',
businessAddress: { country: 'US' },
defaultCountry: 'US',
defaultCurrency: 'USD',
quickCheckoutEnabled: false,
},
},
},
});
});
// Serve static files from the root directory
app.use(express.static(path.join(__dirname, '..')));
// Basic route
app.get('/', (req, res) => {
res.json({ message: 'Welcome to Mom\'s Web API' });
});
// Health check
app.get('/api/health', (req, res) => {
res.json({ status: 'Server is running' });
});
// Routes
app.use('/api/auth', require('./routes/authRoutes'));
app.use('/api/users', require('./routes/userRoutes'));
app.use('/api/products', require('./routes/productRoutes'));
app.use('/api/cart', require('./routes/cartRoutes'));
app.use('/api/orders', require('./routes/orderRoutes'));
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ message: 'Internal Server Error' });
});
// 404 handler
app.use((req, res) => {
res.status(404).json({ message: 'Route not found' });
});
// Start server
const PORT = process.env.PORT || 5000;
const HOST = process.env.HOST || '0.0.0.0';
app.listen(PORT, HOST, () => {
console.log(`Server running on http://${HOST}:${PORT}`);
});