Introduction to MongoDB

Running a Server Q&A

Scott Moss

Scott Moss

Superfilter AI
Introduction to MongoDB

Check out a free preview of the full Introduction to MongoDB course

The "Running a Server Q&A" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:

Questions are asked about how to prevent a server from dying, using the same code on the frontend, and progressive web apps are briefly discussed.


Transcript from the "Running a Server Q&A" Lesson

>> Scott Moss: Any questions on this?
>> Speaker 2: So this is the production-wise, that's how it's been-
>> Scott Moss: Yeah, you can take this right now, build your app, go to Silicon Valley, raise 100 million. Tell them you got this note taking app, but, no, seriously, this is a really good foundation to creating a production app.

Yeah, I mean, it's just one file and it's not really doing anything. But you'll be surprised how similar production code looks to this. There's this more business logic, right, around like I said, authentication and stuff like that. When you get down to business, it's a route that's hitting a database and then returning the result, right, and the difficulty is gonna depend on, how did you model your data?

Did you model your data in such a way that you have to make multiple queries? How did you do that? So that's difficult these queries are gonna be. So yeah, at the end of the day, this is it. This is why I like Node, because you got this little thing that's running, but it's powering so much.

>> Speaker 2: So Node is always running?
>> Scott Moss: Yeah, I opened up a port, I opened up a socket. I'm on 5000. It's just running until it crashes.
>> Speaker 2: [INAUDIBLE]
>> Scott Moss: Yeah, right, yeah, so if you compare this to Apache, man, Apache, I've used Apache before [LAUGH] yeah.
>> Speaker 2: I'm just saying, this looks simpler.

>> Scott Moss: It's way simpler than Apache. Apache is way more low level, right. Express takes care of, you don't have to worry about connecting to the socket, open it yourself and chunking the data and all that stuff. It just takes care of it for you because it's event based.

So yeah, it's a little more high level, but at the end of the day, it kinda does the same thing, yeah. You got a question?
>> Speaker 3: Yes, I think you just answered it. So I was wondering if you just open the connection and not close it, you might get socket exception later.

>> Scott Moss: Yeah, you don't-
>> Speaker 3: Like after a couple of hundred requests.
>> Scott Moss: Yeah, you want get that here. [LAUGH] I know what you are talking about. I ran into that so many times. Yeah.
>> Speaker 2: How do you prevent the server from dying, you have means to handle everything, but in the real life, it's-

>> Scott Moss: Yeah, so if you wanna know more about that in detail, the course I taught yesterday talks a little more about that. But the API course that I taught talks about that a lot. But in short, if you don't want the server to die, then you just gotta catch your errors, all right?

Cuz I have throw an error in here, the whole process is gonna crash, and the server crashes. And then you gotta figure out, did I want this error to cause a crash? Or did I not want this error to cause a crash? If you are not authenticated and that caused an error to my server, I don't want that to cause a crash, it's you're fault you're not authenticated.

>> Speaker 2: You may have external tools to be, like I'm thinking of doing something to, double-checking that there's something is still running or I don't know.
>> Scott Moss: Yeah, so like a health check service or something like that. Yeah, you can register a help check route. At the end of the day, you just gotta be wary of what's coming in.

Just like any other server, you just need to prevent the errors from happening but most of the time, you'll put something in front of this. So like if you did that you do like engine x for reverse proxy or sometimes a caching layer or a CDN, but most times there's something in front of it.

So this thing is just focused on data, which is Jason. At that point, if it's just Jason, you can kinda have full control over it, yeah.
>> Speaker 2: [INAUDIBLE] other things, but I don't know if it's all related, I'm just wondering, [INAUDIBLE] back and front end, I mean you can reuse the same things on the front-

>> Scott Moss: There you go, yeah, you can. So if you ever built an app that was like isomorphic or universal, yeah, you were using the same code. Like you were literally-
>> Speaker 2: Well those are things you could use on the front, yeah.
>> Scott Moss: Yeah, you can use it on the front end, I mean you couldn't make an express server on the front end, but a lot of this code, you could use on the front end.

I mean technically, you could use Mongoose on her front. And actually they have examples of how you can do it, Adam. Yeah, I know, right. Some people find theirself in the position of where they're like type checking data on the front end and they love the schemas of Mongoose that they use that for validations.

>> Speaker 2: Because exactly, the evaluations, it would be so nice to be able to have it back and front, the same.
>> Scott Moss: Yeah, I've seen people do it. If you ever heard something called Meteor, that's literally what that does. So yeah. You had a question, Mark?
>> Speaker 4: I was just gonna comment, PWAs and offline.

If you're gonna save all your notes offline in a service order or something, and then when you connect, then you can cut off the data to the API or whatever. A lot of people will do that, so they'll mock.
>> Scott Moss: Exactly. So he's talking about progressive web apps, which are like the new mobile apps, offline storage and all that stuff.

So there's some advance techniques there, where you will use a lot of that, a lot of those techniques on the front end than you would on the back end for syncing the server and the data. So yeah, it all is just JavaScript. But the thing about is Node itself has things that you can't do in the browser, that you can do-

>> Speaker 4: Yeah, I'm talking mostly about the models, and [INAUDIBLE]-
>> Scott Moss: Yep, you can do a lot of that if you want to.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now