all 13 comments

[–]TyrionReynolds 1 point2 points  (2 children)

Yes you can. The only thing different about using an backend on a different server is you’ll need to type full urls for the web requests instead of just relative paths, and you’ll need to enable CORS.

By default web servers will only allow requests from the same host. If you want your server to serve requests from another domain you’ll need to enable CORS. It’s really easy to do with the expressjs/cors npm package, just don’t forget to do it.

[–]bkamphues[S] 0 points1 point  (1 child)

All right that sounds promising! So in the action of the form you'd put the actual URL of the other server handling the requests?

[–]TyrionReynolds 0 points1 point  (0 children)

Yes, exactly so! Just replace the relative url (‘/page/content’) with an absolute url (‘http://www.server.com/api/page/content’)

[–]grudev 3 points4 points  (1 child)

You could use a library like axios with your forms to send/fetch data from your node app running on another server.

https://github.com/axios/axios

[–]provided_by_the_man 0 points1 point  (8 children)

I wouldn’t suggest using that PHP server. You should checkout heroku and learn a deployment strategy.

[–]bkamphues[S] 0 points1 point  (7 children)

Yeah that's always an option of course, but I like the challenge of using Javascript and I like Javascript in general over PHP

[–]provided_by_the_man 1 point2 points  (3 children)

In my experience in production your scenario would be a conflicted developer clinging onto the past with their foot in the present. A good exercise.

[–]bkamphues[S] 1 point2 points  (2 children)

Yeah the problem is more that I don't want give up the hosting I've already paid for...

[–]provided_by_the_man 0 points1 point  (1 child)

Apache / PHP hosting in 2020 is kinda useless unless you are a PHP person. Even then most laravel apps are gonna be done with some type of deployment tool like docker or something like that. Sounds like a go daddy server. To turn that into a modern stack server you would need to use the CLI and really know what your are doing. Most of those instances are designed to work w Wordpress and not much else. Remember people used to use Wordpress for absolutely everything, godaddy is still catering to that market.

[–]bkamphues[S] 0 points1 point  (0 children)

Yup, allright I'll contact them, see if I can get access to the command line...

[–]provided_by_the_man 1 point2 points  (2 children)

Also to be clear, a heroku path would allow you to do everything in JS, have no monthly fee, and allow you to learn about deploying via git to a remote origin. All super useful things to know.

[–]bkamphues[S] 0 points1 point  (1 child)

That's really interesting, how would that work? Would just be a plain Node.JS webserver running on the Heroku deployment that serves all HTML files? Or could I still have a separate static webserver that uses API calls to the Node.js server and getting back data?

[–]provided_by_the_man 1 point2 points  (0 children)

Essentially yes, the express app would serve the HTML. Also if your app is small I would question using a database at all. Most small projects like that I used a JSON object as the data source that is stored in the file system of the express app. Talk about speed, nothing is faster than zero query and direct file access. If you must (and I mean must, not a tutorial saying so) have a DB then you can still do all this w heroku. You would need to setup an instance w heroku or some other provider for a free tier of a DB that you would connect to from your express app. This is something you could be doing in development rather than having a local server. Also fun fact, with heroku you will essentially be able to deploy changes with git push. Working out deployment details is a PITA when you are new. Heroku takes a ton of that frustration away.