API calling within Needle

Hey guys, I’ve looked after some questions inside the forum, but I have a doubt regarding APIs. I can’t seem to make my needle project initialize my server.

I know it’s using Vite on the project for building and I’m using Express.js and axios for the API calls, but I don’t know where is it being initialized, since I can’t make my server.ts be called in the beginning. Can you guys help me on setting that?

Original Post on Discord

by user 185426914486386688

Hello Bruno. The vite template does run a local webserver (managed by vite) for development. It does not automatically start a second express server. If you want to start another project automatically you could try modifying the start script in the package.json for example.

This is what is run to start the local server when you click play in Unity (besides exporting etc)
"start": "vite --host"
So you could prepend or append other commands like node xyz and chain them. Like so vite --host && node xyz (or instead of node xyz it could be running another script npm run xyz or npm start --prefix path/to/your/express/server)

Oh, okay! Thanks @marcel :cactus: !

by user 185426914486386688

@marcel :cactus: , another thing: When trying to implement my express server, I’m getting an error just like Cannor read properties of undefined (reading 'from') and it’s just when I try to run express within needle with Typescript. When running with Javascript, it doesn’t even start my server, because it just runs to another instance and it doesn’t let my server even start. How can I solve this issue, because it’s something I’m actually stuck, because I have an entire API ready in express and I can’t import it to Needle because those issues are stopping me to do so.

by user 185426914486386688

What do you mean “import to needle”? You can not “import” an express server.

If you have a express server with an api that you want to use then you have to:

either modify your project (our template uses vite which does for local dev start a simple http server and for building it builds static html files which you can then “serve” from either some normal http server or possibly your express server


run both the vite project as well as the express server separately, make sure to setup CORS so they are allowed to communicate, and then just do your API calls from the vite project

When I said “import to needle”, I said bringing my code to my needle project, setting up my express to run separately from needle, but it looks like my express server is being overlapped by needle’s configs.

by user 185426914486386688

Why would you want to merge the express server into the same project/package ? This sounds troublesome

Because I need to make some API calls to reproduce video from an external source, which will be automated to run on my platform.

For example: I need to get a request from said platform that will give me an userId, a SessionId and an array of videoId’s to reproduce them on my platform. I thought of making an API with Express to handle those requests, even when creating separate user sessions for my platform (for example, creating one room for each user).

by user 185426914486386688

I think you’re mixing up client side and server side code here

Is your goal that this runs standalone on one Glitch instance? Otherwise just deploy your express server as usual (e.g. a separate Glitch project or elsewhere) and fetch data from it / post to it from your client-side Needle project

Hello @Bruno Dias here’s also some documentation on vite which you might find useful if you want to integrate the express server with your vite project (for example to serve the client side files using your express server): Backend Integration | Vite

Oh, all right! Thanks a lot!

by user 185426914486386688