1. Home
  2. Explore
  3. Computer Science
  4. Node.js Express API

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Node.js Express API

  • 42Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Backend Technologies
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is the primary purpose of Node.js in web development?

Tap to flipClick to flipspace

Node.js allows JavaScript to be used on the server side, enabling the development of server-side applications.

Or swipe the card

42 cards

  1. What is the primary purpose of Node.js in web development?

    Node.js allows JavaScript to be used on the server side, enabling the development of server-side applications.

  2. What is Express.js?

    Express.js is a web application framework for Node.js, used for building web applications and APIs.

  3. What database technology is used for data storage in this project?

    MongoDB is used as the data store in this project.

  4. What is Mongoose.js?

    Mongoose.js is an ODM (Object Data Modeling) library for MongoDB and Node.js.

  5. What command-line tool is recommended for use in this project on Windows?

    Git Bash, which comes with Git for Windows, is recommended for the command line.

  6. What is the initial file structure created for the backend service?

    The initial file structure includes a main 'app.js' file and a 'models' folder.

  7. How is a package.json file created for a Node.js project?

    A package.json file can be created using the npm init command in the terminal.

  8. What are the initial dependencies added to package.json?

    The initial dependencies are Express, body-parser, and Mongoose.

  9. How are project dependencies installed after defining them in package.json?

    Dependencies are installed by running the npm install command in the project directory.

  10. What is the purpose of body-parser middleware in an Express application?

    Body-parser is used to parse incoming request bodies in a middleware before your handlers, making them available under the req.body property.

  11. How do you connect to a MongoDB database using Mongoose?

    You connect by using mongoose.connect() and providing the database URI, such as mongodb://localhost/bookstore.

  12. How do you define a basic GET route for the homepage in Express?

    You define a GET route using app.get('/', function(req, res) { res.send('Hello World'); });

  13. What is the purpose of res.send() in an Express route?

    res.send() sends a generic HTTP response to the client.

  14. How do you configure an Express application to listen on a specific port?

    You use app.listen(port, callback) (e.g., app.listen(3000, function() { console.log('Running on Port 3000'); });).

  15. What tool can be used to automatically restart a Node.js application when file changes are detected?

    Nodemon is used to automatically restart the Node.js application on file changes.

  16. How do you start a Node.js application using Nodemon?

    You start it by running nodemon app.js or simply nodemon in the project directory if app.js is the default entry point.

  17. How do you access the MongoDB shell from the command line?

    Navigate to the bin directory of your MongoDB installation and run the mongo command.

  18. How do you switch to or create a new database in the MongoDB shell?

    You use the command use databaseName, for example, use bookstore.

  19. How do you create a new collection in the MongoDB shell?

    You use the command db.createCollection('collectionName'), for example, db.createCollection('books').

  20. How do you insert a document into a collection in the MongoDB shell?

    You use db.collectionName.insert(documentObject), for example, db.genres.insert({ name: 'Suspense' }).

  21. How do you retrieve all documents from a collection in the MongoDB shell?

    You use db.collectionName.find(), for example, db.genres.find().

  22. What is a Mongoose Schema?

    A Mongoose Schema defines the structure of documents within a MongoDB collection and the types of data that each field can hold.

  23. How is a create_date field with an automatic timestamp defined in a Mongoose schema?

    It is defined with type: Date and default: Date.now.

  24. How do you export a Mongoose model to make it accessible from other files?

    You use module.exports = mongoose.model('ModelName', schemaName), for example, module.exports = mongoose.model('Genre', GenreSchema).

  25. How do you create a function within a Mongoose model to get all genres?

    You define a method in module.exports for the model, such as module.exports.getGenres = function(callback, limit) { Genre.find(callback).limit(limit); }.

  26. How do you use a Mongoose model function (e.g., getGenres) in an Express route?

    First, require the model, then call the function with a callback: Genre.getGenres(function(err, genres) { if(err) throw err; res.json(genres); });

  27. What is the format for a GET request to retrieve all genres from the API?

    The format is GET /api/genres.

  28. What is the format for a GET request to retrieve all books from the API?

    The format is GET /api/books.

  29. What is the format for a GET request to retrieve a single book by its ID from the API?

    The format is GET /api/books/:_id, where :_id is a placeholder for the book's unique identifier.

  30. How do you define a Mongoose model function to get a single book by its ID?

    You define module.exports.getBookById = function(id, callback) { Book.findById(id, callback); }.

  31. How do you define a POST route to add a new genre in Express?

    You use app.post('/api/genres', function(req, res) { ... }); where req.body contains the genre data.

  32. How do you define a Mongoose model function to add a new genre?

    You define module.exports.addGenre = function(genre, callback) { Genre.create(genre, callback); }.

  33. What HTTP method is used to create new data in a RESTful API?

    The POST method is used to create new data.

  34. When testing a POST request with tools like Rest Easy, what HTTP header is crucial for sending JSON data?

    The Content-Type header should be set to application/json.

  35. What is the format for a POST request to add a new book to the API?

    The format is POST /api/books.

  36. What HTTP method is used to update existing data in a RESTful API?

    The PUT method is used to update existing data.

  37. What is the format for a PUT request to update a genre by its ID in the API?

    The format is PUT /api/genres/:_id, where :_id is the ID of the genre to be updated.

  38. How do you define a Mongoose model function to update a genre?

    You define module.exports.updateGenre = function(id, genre, options, callback) { var query = { _id: id }; var update = { name: genre.name }; Genre.findOneAndUpdate(query, update, options, callback); }.

  39. What happens if not all fields are included in the request body when updating a document using findOneAndUpdate (without specific options like $set)?

    Fields not included in the request body will be overwritten with null or removed, potentially causing data loss.

  40. What HTTP method is used to delete data in a RESTful API?

    The DELETE method is used to delete data.

  41. What is the format for a DELETE request to remove a genre by its ID from the API?

    The format is DELETE /api/genres/:_id, where :_id is the ID of the genre to be deleted.

  42. How do you define a Mongoose model function to delete a genre by ID?

    You define module.exports.removeGenre = function(id, callback) { var query = { _id: id }; Genre.remove(query, callback); }.

Card 1 of 42
Card 1 of 42

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent