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

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

Scott demonstrates how to run the Express server that was just created, and takes questions from students.

Preview
Close

Transcript from the "Running Express" Lesson

[00:00:00]
>> Scott Moss: So if we were to run the server, and hopefully this will work, we should be able to just say node test.js. And I'm gonna stop here, because this should be morgan, there we go.
>> Scott Moss: Why does that do that? [LAUGH]
>> Scott Moss: We'll run that. Okay, so I didn't put a log here so you can't see that my server is running, but it's running on port 5000.

[00:00:37]
And if I do a GET request to port 5000/note, I don't have any notes in the database, so I'd imagine I'd just get back an empty array, which I do, right? Cuz I don't have any notes. And you can see, I got /note 200. I could create some notes, but I'd have to open up a little GUI to do a POST request, or something like Postman.

[00:01:02]
But yeah, that's basically it. So that's how you integrate with an API. I've integrated Mongo with Python before. I've integrated it with Java before. By far the easiest with Node, especially with something like Express. So pretty simple. I use it with GraphQL, it's pretty simple. It's just super flexible.

[00:01:21]
As you can see with the models, you can kinda do whatever you want. A really good pattern is instead of importing all your models into one file, is just attach them to some object that you inject into all your controllers or something. So every controller has every model, versus you trying to figure it out.

[00:01:37]
So in this case, in Express you might attach the models to the request object. You might do something like that so they're all there. So you can just be like request.models.find, whatever, and you can just do that without having to import them all. So really, it depends on how you organize your app.

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