Node.js Express API
- 42Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- 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?
Node.js allows JavaScript to be used on the server side, enabling the development of server-side applications.
Or swipe the card
42 cards
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.
What is Express.js?
Express.js is a web application framework for Node.js, used for building web applications and APIs.
What database technology is used for data storage in this project?
MongoDB is used as the data store in this project.
What is Mongoose.js?
Mongoose.js is an ODM (Object Data Modeling) library for MongoDB and Node.js.
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.
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.
How is a
package.jsonfile created for a Node.js project?A
package.jsonfile can be created using thenpm initcommand in the terminal.What are the initial dependencies added to
package.json?The initial dependencies are Express, body-parser, and Mongoose.
How are project dependencies installed after defining them in
package.json?Dependencies are installed by running the
npm installcommand in the project directory.What is the purpose of
body-parsermiddleware 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.bodyproperty.How do you connect to a MongoDB database using Mongoose?
You connect by using
mongoose.connect()and providing the database URI, such asmongodb://localhost/bookstore.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'); });What is the purpose of
res.send()in an Express route?res.send()sends a generic HTTP response to the client.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'); });).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.
How do you start a Node.js application using Nodemon?
You start it by running
nodemon app.jsor simplynodemonin the project directory ifapp.jsis the default entry point.How do you access the MongoDB shell from the command line?
Navigate to the
bindirectory of your MongoDB installation and run themongocommand.How do you switch to or create a new database in the MongoDB shell?
You use the command
use databaseName, for example,use bookstore.How do you create a new collection in the MongoDB shell?
You use the command
db.createCollection('collectionName'), for example,db.createCollection('books').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' }).How do you retrieve all documents from a collection in the MongoDB shell?
You use
db.collectionName.find(), for example,db.genres.find().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.
How is a
create_datefield with an automatic timestamp defined in a Mongoose schema?It is defined with
type: Dateanddefault: Date.now.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).How do you create a function within a Mongoose model to get all genres?
You define a method in
module.exportsfor the model, such asmodule.exports.getGenres = function(callback, limit) { Genre.find(callback).limit(limit); }.How do you use a Mongoose model function (e.g.,
getGenres) in an Express route?First,
requirethe model, then call the function with a callback:Genre.getGenres(function(err, genres) { if(err) throw err; res.json(genres); });What is the format for a GET request to retrieve all genres from the API?
The format is
GET /api/genres.What is the format for a GET request to retrieve all books from the API?
The format is
GET /api/books.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:_idis a placeholder for the book's unique identifier.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); }.How do you define a POST route to add a new genre in Express?
You use
app.post('/api/genres', function(req, res) { ... });wherereq.bodycontains the genre data.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); }.What HTTP method is used to create new data in a RESTful API?
The POST method is used to create new data.
When testing a POST request with tools like Rest Easy, what HTTP header is crucial for sending JSON data?
The
Content-Typeheader should be set toapplication/json.What is the format for a POST request to add a new book to the API?
The format is
POST /api/books.What HTTP method is used to update existing data in a RESTful API?
The PUT method is used to update existing data.
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:_idis the ID of the genre to be updated.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); }.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.
What HTTP method is used to delete data in a RESTful API?
The DELETE method is used to delete data.
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:_idis the ID of the genre to be deleted.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); }.

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