Text-to-Speech CORS policy issue + 404 error when creating node index.js to solve it

Question: When trying to deploy the PlayHT text-to-speech API, I get the following error in the Chrome browser console:

When I make the following 2 changes to address it, I get a 404 access “cannot GET /” error:

  1. Change 1: Create an index.js file as specified in this solution to the problem.
  2. Change 2: Modify the folder structure such that all client side files are in a folder call public (e.g. script.js, style.css, index.html), and the index.js is outside this public file in the root directory. Modify path references accordingly. Run node index.js

Any ideas on where I begin to debug? Have spent a day on this now :frowning:

Repl link:
https://replit.com/@IshaniM1/EdTech-Chat-InterfaceMobile

Code in the index.js file

const express = require("express");
const app = express();
const http = require("http");
const sockets = require("socket.io");
const cors = require("cors");

app.use(cors({
  origin: "https://edtech-chat-interfacemobile.ishanim1.repl.co",  // Update with your origin
  methods: "GET",
  preflightContinue: false,
  optionsSuccessStatus: 204,
  credentials: true,
}));

app.use(express.static("public"));


const server = http.createServer(app);

const io = sockets(server, {
  cors: {
    origin: "https://edtech-chat-interfacemobile.ishanim1.repl.co",
    methods: ["GET", "POST"],
  },
});

app.get("/api/connection-check", (req, res) => {
  res.json({
    server_status: "good",
  });
});

app.get("/", (req, res) => {
  // Send a response for the root route
  res.sendFile(__dirname + "/public/index.html");
});

const port = process.env.PORT || 8080;
server.listen(port, function() {
  console.log("Listening on port", port);
});

:wave: Hey @IshaniM1, welcome to the forum!

You need to move the code and all the folders to a Node.js Repl, as it won’t work in an HTML/CSS/JS Repl, a special kind of Repl which is just static with no server.

4 Likes