As a web developer, the jump from designing front-end to writing serverside code for the first time can be incredibly jarring. One of the first feelings I had was the lack of grounding. Working on front-end you know when your code works because you experience it. Right there, rendered on the page, there's been a change. But the first time you change write a new endpoint, there's nothing ready to tell you that you've accomplished your goal. For a second, you wonder why you've heard anyway say they prefer this cavernous blackness to the immediate rewards of front-end. But that's just because like the first time you wrote any code, there's tools and tricks you're not aware of yet.
My first rule of backend dev: Postman.
Postman is what's going to turn your backend blackout into a real breathing world of code. Postman is a tool you may have been introduced to before, it allows you to send http requests without the effort of writing ajax, axios or any other code. It's flexible, and dependable. The only code you need to write in your back end to get started is an endpoint, and a response. Once you have that finished, fire up the server with debugger, npm start or any other method and open up postman. Write what you think your endpoint should be on postman, and fire off a request.
If Postman is giving you a 500, you have the wrong address most likely. Check the port your localhost is running on and try again. What we're looking for isn't necisarily a 200 range reply, but 200 or 400 would mean we're on the right track. Play with your request until you get it, and if you're really struggling make sure that your server is really running.
Once you have the sweet sight of a new response, check what number you're getting. Is it a 400? Sure, it doesn't sound like a success message but this is a big deal! you've hit your server. Now it's just formatting, so feel confident. I always do at this point, because this means I'm getting close, and I've narrowed down what to fix.
The next item to look at is your request type. On postman, in the upper left field you can select a type. Get and post are the most common here. What type doesn't matter so much, what matters is matching the type you wrote on the server. At the time, you may not have been considering this, but go look at the endpoint you wrote. Most likely, it's differentiated itself by what type of request its handling. Perhaps "get," or perhaps "post." On postman, match you request type to your endpoints type. Does this give you a 200 yet?
If not, lets look at those "/" after your url.
It's possible depending on how you've built your backend points that your router has some premptive slashes in earlier files. Really closely go through all of your functions on the backend, and see where if at all you have end points written out. If there's none, obviously that's its own problem and you should go back to step one of this guide.
Often times, you'll see the error before you'll ever expirment into it by luck, so again, just look closely.
And of course, now is a great time to see how your back end function is replying. Is it sending a 200 status back? If you didn't write the code to make it, then it's probably not. Worse, did you write code to make it send anything back at all? Typically, you'll need a line like res.end or res.send. If you don't know what I mean res, on your end point you probably didn't put parameters. By default in node and most node frameworks you'll want two parameters on your route
req stands for the request, the one thats managed to find its way to your endpoint. Res stands for result, it's an object with some built in functions and storage waiting for you to tell it what to do. That's where you'll update the status code and call a function to send it back.
Last, if this is your first time to node, make sure you're running your server and running the right file to run your sever. If you're trying to run your server and you're getting an exit code, the basic setup of your server file might be wrong. More likely, you're running the wrong file altogether. You can configure this using your debugger or by navigating to the right file in your terminal if that's how you were starting.
I sincerely hope this has been informative and if you're still stuck please feel free to comment below and ask questions.