Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
<pre>const next = require( 'next' );
const express = require( "express" );
const wooConfig = require('./wooConfig');

const WooCommerceAPI = require('@woocommerce/woocommerce-rest-api').default;

const WooCommerce = new WooCommerceAPI({
    url: wooConfig.siteUrl,
    consumerKey: wooConfig.consumerKey,
    consumerSecret: wooConfig.consumerSecret,
    wpAPI: true,
    version: 'wc/v3'
});

const port = 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare()
.then(()=>{
    const server = express();

    server.get('/getProducts',(req,response)=>{
        WooCommerce.get("products",function(err,data,res){
            response.json(JSON.parse(res));
        });
    });
    
    server.get("*",(req,res) =>{
        return handle(req,res)
    });

    server.listen( port, err => {
        if ( err ){
            throw err;
        }
        console.log(`Ready on ${port}`);
    });
})
.catch(ex=>{
    console.error(ex.stack);
    process.exit(1);
});


What I have tried:

I followed all the steps from the internet and the documentation. But when I try to run http://localhost:3000/getProducts, its just loading and doing absolutely nothing. my http://localhost:3000 is working fine, so its only this endpoint.
Posted
Comments
Greg Utas 29-Sep-21 8:49am    
Assuming your code is correct (I've never used this language), what about your firewall? Do you need to enable port 3000?
Member 15375565 29-Sep-21 9:57am    
Yeah its enabled

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900