Docsity

Node.js Database Development Notes – MVC Controllers, Routes, CRUD & SQL, Assignments of Computer science

Partial preview of the text

Download Node.js Database Development Notes – MVC Controllers, Routes, CRUD & SQL and more Assignments Computer science in PDF only on Docsity!

Edit a Country Note this uses an array of two parameters for the SQL query, placeholders (the “?” character) in the SQL. since there are two [request.body, id], (error, result) => { F (error) { next(error) 5 } else { response. redirect ( Add/Edit database rows using Node Remember, you will first need to make a view that renders the add or edit page with a form in it. Some examples of this are below. Performing a POST Request You will need to create a form. The action attribute will contain the URL to a route you have configured in your routes script. The input fields inside the form will need to be named to match your database columns. These are used in the controller to add, edit, or delete rows from the database. See below for an example form - this is configured to add countries to the database. You may need to edit the input field name, among other things.
Name:
Add a Country Add the following code to your countries controller. { , request.body, (error, next (error); } else { response. red: Remember to add the function to the module.exports area. You will need a route using the POST method that uses the above function as its callback: app-post( Get a Country by its ID The fifth route allows to you view a specific country. To do this, complete and modify to the example. The code starts as shown, but we are missing the database lookup code. id’, rseInt(reg. db get ( * FROM c (idl, if (err) { Remember to call the country. ejs template, passing data on the id of the country. You will also need to create the template, placed here in Teams, e.g Then, test your changes in a log location at: http-/localhost:3000/countries/1 where 1 is an ID of a country in your database. Challenge Now attempt to complete some more routes on your own, by tinkering with your controller file. For example: * Add a route to add a new country © Add 2 route to update an existing country Add a route to delete a country When you have finished, test your application to make sure all the routes work correctly and the data is saved to the database. i fi EJS supports to use the config connections in your database. Alternatively, check your file for those includes. Module Exports When you use require in another script, it gives you access to the functions within that script. Instead only the functions that have been added to module .exports. As you add new functions to your controller, make sure you add them to the exports. This will ensure controllers are they only access the function name, as exposed in as follows: This approach keeps your controller organised and ensures you only expose the functions you want to be available to other parts of your application. Once you've created the controller, added the route, and set up the view, test your application by running it and navigating to the countries page. You should see a list of countries from your database, rendered using the EJS template.