Guide on deploying a web application using Socket.io and SvelteKit on Vercel

Currently, I'm developing a multiplayer .io-style game that utilizes both Socket.io and SvelteKit. While testing the project on a local development server, everything runs smoothly. However, upon deploying to Vercel, an error message stating

Failed to load resource: the server responded with a status of 404 ()
is logged.

Below is my current express server.ts code snippet (not functional)

import http from "HTTP";
import { handler } from './build/handler.js';
// Encounters an error: An import path cannot end with a '.ts' extension.
import injectSocketIO from "src/lib/socket-handler.ts";
import express from 'express';

const app = express();
const server = http.createServer(app);

injectSocketIO(server)

app.use(handler);

// Is there something that needs to be modified here?
server.listen(3000, () => {
    console.log('Running on http://localhost:3000');
});

To run a local development server using the vite plugin, I input the following:

import injectSocketIO from "./src/lib/socket-handler";

plugins: [sveltekit(), {
    name: "sveltekit-socket-io",
    configureServer(server) {
        injectSocketIO(server.httpServer);
    },
}],

I have attempted to follow the guidelines provided here. However, I am using socket-handler.ts instead of socket-handler.js and am unsure about how to establish a startup script for execution with Vercel. Any assistance on this matter would be greatly appreciated!

Answer №1

After much searching, I found a solution that worked for me. By moving the Express server outside of the Svelte src directory, I was able to resolve the same issue.

My approach mirrored yours, with the only difference being that I imported socket-handler.js from the root directory:

// server.js
import express from 'express'
import injectSocketIO from './socket-handler.js'
import { handler } from './build/handler.js'

const app = express()
const server = http.createServer(app)

// Integrate SocketIO
injectSocketIO(server)

// Handle SvelteKit requests
app.use(handler)

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000')
})

To implement this change, simply run

npm run build && node server.js
to compile the Svelte app and launch the Express server.

This guide pointed me in the right direction. Essentially, by setting up a traditional Express server, integrating socket.io, and leveraging SvelteKit's Express middleware capabilities, the issue was successfully addressed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Step-by-step guide on generating an index through mongoose and elastic search in a node.js and express.js environment

I am looking to set up the index in elastic search using mongoose and express, but I have not been able to find any documentation on how to do it. I attempted to use mongoosastic, but it did not meet my needs. Is there anyone who can assist me with this? ...

Despite the unconsumedBufferLength being 0, DataReader.loadAsync is still being completed

Working on UWP WinRT, I'm dealing with JSON stream consumption using the following code: async function connect() { let stream: MSStream; return new CancellableContext<void>( async (context) => { stream ...

Obtain the total number of requests submitted by the user within a 24-hour period

I have a POST request point in my API where I need to track and store all work journals made by a worker. export const registerPoint = async (req: Request, res: Response) => { const user = res.locals.decoded; const {id} = user; const point = new Point ...

Sorting the order of items for display through the Dropdown menu feature in Bootstrap

I'm working on an app that showcases recipes on the main page (mywebsite.com/recipes). I'd like to give users the option to sort the recipes by either date or popularity, with a button right on the page for easy selection. Currently, I'm usi ...

Encountering an error when attempting to show user details on a webpage using Angular and Ionic with Promise functionality

On my app's AccountSettingsPage, I am fetching user data from a SQLite DB and displaying it on an Ionic page. However, I encountered the following error: Error: TypeError: Cannot read property 'name' of undefined at Object.eval [as upd ...

Develop a JSON structure by retrieving nested documents using Firebase Firestore and Express

I'm currently working on developing an application using Express and Firebase Cloud Functions. I'm facing a challenge in creating a nested JSON structure based on the database schema specified below: https://i.sstatic.net/2vI8z.png Below is the ...

Strategies for handling uncaught promise rejections within a Promise catch block

I'm facing a challenge with handling errors in Promise functions that use reject. I want to catch these errors in the catch block of the Promise.all() call, but it results in an "Unhandled promise rejection" error. function errorFunc() { return ne ...

The Radio Button's value appears in a distinct way on Ionic Angular

I am currently working with the Ionic framework and I am trying to display data values on radio buttons. However, I am facing difficulties in retrieving the correct value and setting it appropriately. index.html <td> <label>{{learn ...

Nodemon has encountered an issue: Unable to listen on port 5000. The address is already in use

During the creation of my project with nodejs and express for the backend, everything was running smoothly. However, whenever I made changes to a file, nodemon would encounter an error preventing the server from restarting: Error: listen EADDRINUSE: addre ...

Would it be secure to store the Express Session Secret as plain text while using it with Angular inside a Docker Container?

Upon taking over a new project, I noticed that the front end docker container has been set up in the following manner. Although this may seem like a basic question, I am still getting the hang of working with Angular/Express/Nodejs. FROM node:18.12.1 ...

Implement a logging system to track and record data from both incoming requests and outgoing responses on a server powered by Express and Node.js

Is there a way for my server to log the response and request data when posting to another server? Thank you. const request = require('request'); postToIotPlatform = function postToIotPlatform(req, res, next) { var formData = JSON.stringify( ...

Trigger the script upon clicking the Save button within the AdminBro Edit page

AdminBro's documentation mentions that they provide predefined actions Edit (record action) - update records in a resource They also offer a hook called after. I created a function and assigned it to MyResource.edit.after. However, the issue is tha ...

The validation of DOM nesting has detected that a <td> element cannot be placed within an <a> element

When working on a React project with Material UI, I encountered an issue while trying to create a table. My goal was to make the entire row clickable, directing users to a page with additional information on the subject. Below is the snippet of code for th ...

Exploring the power of Vue.js reactivity using Object.defineProperty in a TypeScript environment

Currently, I am in the process of developing a TypeScript class to manage form submissions and handle server errors: export default class Form<Model> { private readonly original: Model private changes: Partial<Model> constructor(d ...

Transform the image retrieved from loopback-component-storage

Using loopback, I am storing images on the server. I need to alter the file name of the image before it is saved to the server. Additionally, I want to transform it into a thumbnail before saving. This is my current approach: On the client side Upl ...

Utilize Array in Form Input with Index and Spread Operator

Looking to create a form that can handle array data with dynamic fields in TypeScript. Encountering the following error: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ nam ...

unable to exit a function in node.js

I am facing an issue with three blocks of code where the first block executes initially, passes its result to the second block, and then finally sends the final result to the third block which is supposed to send data to the route. However, I am encounteri ...

Best practices for server side countdown implementation

Issue I am facing a challenge with displaying a server-side countdown on the client side. Initially, I set it up using socket.io to emit data every 100 milliseconds. While I like this approach, I am concerned about potential performance issues. On the o ...

Adapt vanilla JavaScript code to be compatible with Node.js, MongoDB, and Express framework

In the midst of working on a blog project for my class, I find myself faced with the challenge of integrating Nodejs, Mongo, and Express into our existing setup. Up until now, we have been gradually transitioning to using Express in our application develop ...

Error: The property 'case sensitive routing' cannot be accessed because it is undefined

Task at hand: Running ExpressJS port using Node.js, nodemon, and lib. Operating System: Windows 10 Home x64 Node.JS Version: Lts The Challenge: Getting the ExpressJS port to run successfully. Current Issue: Encountering an internal file error, potentiall ...