0

I'm a huge sneaker head and getting my feet wet in node and express and I'm trying to create an app using Twilio to text me when a certain shoe decreases in price and the shoe is triggered by replying "Track (SKU)" to add it to the database. I have it completely written and when I input "node index.js", it connects to my localhost just fine, but when I input http://localhost:3000/sms, I get Cannot GET /sms. I've been trying to look for a fix by trying to find some videos or if someone has done a similar project to model off of, but no luck. Is anyone familiar with creating Twilio apps with express servers able to help me out? My current index.js is below.


const SneaksAPI = require('sneaks-api');
const sneaks = new SneaksAPI();

const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: false }));

const MessagingResponse = require('twilio').twiml.MessagingResponse;

const Subscribers = [
    {
      "number": 2025550121,
      "styleID": "FY2903",
      "lastResellPrice": 475
    },
    {
      "number": 1234567890,
      "styleID": "DD0587-600",
      "lastResellPrice": 285
    }
  ];

  app.post('/sms', function (req, res) {
    const twiml = new MessagingResponse();
    const SMS = twiml.message();
    const recievedSMS = req.body.Body.toLowerCase().trim();
    const firstWord = recievedSMS.split(" ")[0];

    if (firstWord == 'track'){
    const styleID = recievedSMS.split(" ")[1] || null;
    if(styleID){
        const sneaker = sneaksApiFunctionWrapper(styleID);
        if(!sneaker){
            SMS.body("Sneaker could not be found");
        } else {
            const sub = {        
                "number": req.body.From,
                "styleID": sneaker.styleID,
                "lastResellPrice": sneaker.price
            };
            Subscribers.push(sub);
            SMS.media(sneaker.image);
            SMS.body(`Current lowest price for ${sneaker.name} is $${String(sneaker.price)} at ${sneaker.site}: ${sneaker.url}\nYou will be notified when the price drops. Reply STOP to opt-out of alerts.`);
        }
    }

    } else {
    SMS.body('To start tracking a sneaker, text \"track\" followed by the sneaker ID.');
    }

    res.writeHead(200, {'Content-Type': 'text/xml'});
    res.end(twiml.toString());
});

function sneaksApiFunctionWrapper(styleID) {
    return new Promise((resolve, reject) => {
        sneaks.getProductPrices(styleID, function(err, product){
            const lowestResellSite = Object.keys(product.lowestResellPrice).reduce((a, b) => product.lowestResellPrice[a] > product.lowestResellPrice[b] ? a : b);
            const sneaker = {
                name: product.shoeName,
                image: product.thumbnail,
                site: lowestResellSite,
                price: product.lowestResellPrice[lowestResellSite],
                url: product.resellLinks[lowestResellSite],
                styleID: product.styleID
            };
            resolve(sneaker);
        }, (errorResponse) => {
            reject(errorResponse);
        });
    });
 }

app.listen(3000, () => {
   console.log('Express server listening on port 3000');
});```
JediSZN
  • 59
  • 6

1 Answers1

3

You are using a POST request via app.post(), which is different from a GET request. You'll need to submit data via a form or apps like Postman to properly access the POST request.

When should I use GET or POST method? What's the difference between them? This gives a solid response.

lizziepika
  • 3,231
  • 1
  • 14
  • 23
MTN
  • 537
  • 3
  • 10