DEV Community

Cover image for Beginner's guide to creating a Node.js server

Beginner's guide to creating a Node.js server

Lisa Jung on August 29, 2020

As full stack developers, we use multiple programming languages to build the frontend and backend of our apps. I often find myself mixing the synta...
Collapse
 
jornvm profile image
Jornvm

Great writing for educational purposes! Going trough this was my first encounter with both node.js, backend and using wsl 2. I got stuck on the last part, console returns my input but does not create the user_mood.txt.
I also get a DeprecationWarning for calling an asynchrounous function without callback, so i guess im going to be spending my saturday figuring that out :)

Thank you for this guide :D

Collapse
 
crispy1260 profile image
Christopher Payne

Hey Jornvm,

When I was running through this, I got the following error in my console referencing the fs.writeFile section.

mood=Excited
fs.js:144
  throw new ERR_INVALID_CALLBACK(cb);
  ^
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
    at maybeCallback (fs.js:144:9)
    at Object.writeFile (fs.js:1325:14)
    at IncomingMessage.<anonymous> (E:\Users\Christopher\Documents\GitHub\All_The_Feels\routes.js:30:10)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1221:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'ERR_INVALID_CALLBACK'
}

In my troubleshooting, I added some error handling and it started working for me. I don't understand why adding error handling to that function helped but it allowed the file to be created.

Looking at Lisa's Github repo (linked below), my line 24 of routes.js now looks like:

fs.writeFile("user_mood.txt", mood, function (err) {
    if (err) {
        return console.log(err);
    }
    console.log("File saved successfully!");
});

Hopefully, this gets it to work for you too! I assume it's a copy/paste fail on my part and I am overlooking the error in my file but this worked for me.

Collapse
 
malanius profile image
Malanius Privierre

Hi, this probably depends on the version of Node the post was written and version of Node you're using. The callback parameter is required since Node.js v10.0.0, see docs history where this is mentioned: nodejs.org/api/fs.html#fs_fs_write...

Thread Thread
 
lisahjung profile image
Lisa Jung

Malanius! Thank you for sharing your insights with everyone. One of the many things I love about the developer community is how generous and invested they are in other developers' growth. It feels really good to see readers helping other readers!! :)

Collapse
 
lisahjung profile image
Lisa Jung

Hey Christopher!
Thank you so much for taking the time to share your troubleshooting tips with everyone. I appreciate you!!

Collapse
 
lisahjung profile image
Lisa Jung

You are so welcome Jornvm. I am just getting started on Node.js myself and it's so exciting to hear that you are delving into it as well.

Hm... I haven't encountered that issue while I was building the server. Have you tried comparing your code to the my GitHub repo for this tutorial? github.com/LisaHJung/Node.js_Tutor...

You probably figured it out already lol but let me know how it went!

Collapse
 
andrewbaisden profile image
Andrew Baisden

Cool guide just a few things. You can add syntax highlighting to your code blocks in markdown it will make the article easier to read. Also when working with API's its good practice to use an API tool like postman.com/ or insomnia.rest/

Collapse
 
lisahjung profile image
Lisa Jung

Thank you so much for your great advice,Andrew! I totally agree about using Postman. I usually use Postman but wanted to shake it up this time and try something different. lol I had no idea you can add syntax highlighting to my code blocks in markdown. I will Google how to do it and apply it to my blog next time!

Collapse
 
james245332 profile image
James245332

OK so just send me your hangout email so we can talk

Collapse
 
thomassalty profile image
Thomas Soos

I know it's a beginners' guide but how should I imagine an experts' guide? Is this what professional back-end developers do most of time? Writing these "server.js" and "routes.js" files? I'm just trying to understand what it looks like in the real world, outside of "localhost:3000"...

Collapse
 
lisahjung profile image
Lisa Jung

Hey @thomas Soos! What a great question. Professional back end developers are certainly expected to have mastered the skill sets covered in this blog. However, these skills barely scratch the surface of what you do as a back end developer!

The responsibilities of a back end developer may differ depending on the company he/she works for. In general, back end developer focuses on the logic necessary to make the app function quickly and efficiently. They also develop and manage database to store and secure info needed to run the app. Also, adding Auth and other security measures to your app are definitely important aspects of keeping your app secure.

Some of the skill sets that professional back end developers may focus on are security compliance and accessibility. database administration, managing version control and also the ability to work with front end developers to ensure that front and back end of the app are working together seamlessly.

Hope this helps!

Collapse
 
thomassalty profile image
Thomas Soos

Thanks a lot for the quick reply @lisahjung !

It certainly helps, but now I've got 3 more questions if you don't mind me asking 😀

1, I've thought accessibility is something that front-end developers work on. For example using semantic html, using alt texts for images and aria-labels for screen readers, etc, etc. Or, if you meant accessibility in terms of speed and performance of a website, I think that's also a front-end developer's responsibility by keeping the number of requests low, minifying and compressing necessary files, using CDNs, etc. So my question is:
What do you mean by "back end developers may focus on accessibility"? What do they do to improve it?

2, What if Auth and other security measures have already been added as well as a database? Or even when there's no database? I understand, that no website or webapp is "done" but what if these security measures have been added, they work fine and there are no security issues? What do back-end developers do in that case? Are they just going to start to work on a new app or website?

3, How does a node.js server look like in the real world? Should I imagine a normal server computer that has a more complex "server.js" stored in it? What port does it listen to? If it also listens to port 3000 or any other port, why don't we have to add the port number on URLs the same way we did with localhost:3000?

Once again, thanks a lot for your help! I do appreciate it 😉

Collapse
 
dmartensson profile image
David Mårtensson

I have one objection to the description.

You write that javascript runs on the V8 engine, but you fail to clarify that this is just one possible engine.
If I as a reader know nothing about it I would get the impression that javascript was invented by google and that V8 is the only way to run it :)

I know its under note.js history, but I would clarify that node.js selected the V8 engine from google to run Javascript out of the different existing ones.

Collapse
 
lisahjung profile image
Lisa Jung • Edited

@david Martensson! Thank you so much for letting me know. I am still new to Node.js and I have a lot to learn! I made the change. Thanks again. :)

Collapse
 
malanius profile image
Malanius Privierre

Thank you for the excellent post. I've been preparing a presentation for our regular back-end chapter meeting. Although working with Node for a long time, it was not easy to sum it up for devs that grew on java based backends.
Your post and beginner view helped me a lot to put it together! 😊

Collapse
 
lisahjung profile image
Lisa Jung

Oh wow! You are so welcome Malanius. Your post put a huge smile on my face. Thank you so much for such a wonderful message and for inspiring me to continue writing. Hope you have the most wonderful holiday weekend! :)

Collapse
 
tracy02022 profile image
DONGYU LI

Hi Lisa,
Very like your post, but seems like you forget to set a callback function there for fs writer ,
I am fixing it like this
fs.writeFile("user_mood.txt", mood, () => {});
Thanks,
Tracy

Collapse
 
lisahjung profile image
Lisa Jung • Edited

@Dongyu LI, Tracy!!! Thank you so much for your help. I just made the change you suggested in my blog and in my GitHub repo. Sorry for the delayed response. I cannot believe I didn't see your comment till now.

Thank you so much for helping me and my readers. You are wonderful!!

Collapse
 
hugofeijo profile image
Hugo Feijó

incredible writing, congratulations

Collapse
 
lisahjung profile image
Lisa Jung

Oh wow, thank you so much for such a wonderful compliment Hugo!! You made my day. :)))))

Collapse
 
ladyfantasy profile image
Lady Fantasy

Great tutorial, very useful! Thank you!

Collapse
 
lisahjung profile image
Lisa Jung

You are so welcome Soledad. I am so glad I could help!! :)