{"id":7620,"date":"2023-10-17T17:56:36","date_gmt":"2023-10-17T10:56:36","guid":{"rendered":"https:\/\/www.briswell-vn.com\/?p=7620"},"modified":"2023-10-17T17:58:09","modified_gmt":"2023-10-17T10:58:09","slug":"sails-js-mvc-framework-node-js","status":"publish","type":"post","link":"https:\/\/www.briswell-vn.com\/en\/news\/sails-js-mvc-framework-node-js\/","title":{"rendered":"Sails.js | MVC Framework for Node.js"},"content":{"rendered":"<p><\/p>\n<h1><\/h1>\n<h1>Introduction<\/h1>\n<p><span style=\"font-weight: 400;\">Sails.js is a popular MVC (Model-View-Controller) framework built on top of Node.js that simplifies building scalable and real-time web applications. In this tech blog, we will create a web app with Sails.js and set up a MySQL database connection. Additionally, we will compare Sails.js with Express.js, another widely used Node.js framework.<\/span><\/p>\n<p>Sails.js is actively being used in the wild by companies like <a href=\"https:\/\/www.postman.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Postman<\/a>,\u00a0<a href=\"https:\/\/paystack.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Paystack<\/a>, and <a href=\"https:\/\/devmountain.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">DevMountain<\/a>\u00a0to build out their Web APIs to power their various clients.<\/p>\n<h1>Core Principles of sails.js<\/h1>\n<p>Convention over Configuration: Sails.js provides default values and sensible conventions to minimize configuration, this approach allows developers to focus on building their application&#8217;s business logic instead of spending time on repetitive configuration tasks.<\/p>\n<p>Model-View-Controller (MVC) Architecture: Sails.js follows the MVC architectural pattern, which helps in organizing code and separating concerns. The model represents the data and handles database operations, the view handles the presentation logic, and the controller acts as the intermediary between the model and the view, handling the application&#8217;s business logic.<\/p>\n<p>Automatic RESTful APIs: One of the key features of Sails.js is its ability to automatically generate RESTful APIs based on the defined models. By simply creating a model, Sails.js generates CRUD (Create, Read, Update, Delete) routes and controller actions, making it easy to build API endpoints for data manipulation.<\/p>\n<p><span style=\"font-weight: 400;\">Real-time Communication: Sails.js provides built-in support for real-time communication through WebSockets. This allows developers to build applications that can push data updates in real-time to connected clients. Sails.js utilizes the concept of &#8220;pub\/sub&#8221; (publish\/subscribe) to enable real-time messaging between clients and the server.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Database Flexibility: Sails.js is database-agnostic, meaning it supports various databases such as MySQL, PostgreSQL, MongoDB, and more. Developers can choose the database that best suits their application&#8217;s requirements and seamlessly integrate it with Sails.js using the Waterline ORM (Object-Relational Mapping).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Scalability and Performance: Sails.js is designed with the goal of easy scalability. It provides features like clustering and horizontal scaling, allowing multiple instances of the application to handle high traffic loads. Sails.js also offers performance optimizations, such as caching and asynchronous processing, to enhance the overall performance of the application.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Extensibility and Ecosystem: Sails.js has a vibrant ecosystem with a wide range of plugins and modules developed by the community. These plugins extend the functionality of Sails.js and provide additional features and integrations. The scalability of Sails.js allows developers to leverage the ecosystem to enhance their applications.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">By embracing these core principles, Sails.js empowers developers to build scalable, maintainable, and real-time web applications efficiently. It streamlines the development process by providing conventions, automatic API generation, real-time communication, and database flexibility, enabling developers to focus on creating robust and feature-rich applications.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h1><b>Getting Started with Sails.js<\/b><\/h1>\n<p><span style=\"font-weight: 400;\">Ensure that you have Node.js installed on your machine. You can download the latest version from the official Node.js website (<\/span><a href=\"https:\/\/nodejs.org\"><span style=\"font-weight: 400;\">https:\/\/nodejs.org<\/span><\/a><span style=\"font-weight: 400;\">).<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><b>Install Sails.js<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Open your terminal or command prompt and run the following command to install the Sails CLI tool globally on your system.<\/span><\/p>\n<pre>npm install -g sails<\/pre>\n<p>To verify that Sails is installed run:<\/p>\n<pre>sails -v<\/pre>\n<p>The above command should return a version number if everything went successfully.<\/p>\n<h3>Creating a new Sails application<\/h3>\n<p>To create a new Sails application, run the\u00a0<code>sails\u00a0new<\/code> command passing in the name of your application. This command will both generate a new Sails app and run\u00a0<code class=\" prettyprinted\"><span class=\"pln\">npm install<\/span><\/code>\u00a0to install all dependencies for you.<\/p>\n<p><span style=\"font-weight: 400;\">Navigate to the directory where you want to create your Sails.js project using the terminal or command prompt. Then, run the following command:<\/span><\/p>\n<pre>sails new my-project<\/pre>\n<p><span style=\"font-weight: 400;\">You&#8217;ll see a prompt to choose your project template<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7607\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04133527\/sails-new-my-project.png\" alt=\"\" width=\"1475\" height=\"286\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04133527\/sails-new-my-project.png 1475w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04133527\/sails-new-my-project-300x58.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04133527\/sails-new-my-project-1024x199.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04133527\/sails-new-my-project-768x149.png 768w\" sizes=\"auto, (max-width: 1475px) 100vw, 1475px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Type 1 (or press enter) to start with our &#8220;Web App&#8221; template: an opinionated starter project that includes essential features like login, password recovery, emails, and billing. Or, if you want to start from scratch with an empty project, choose 2 for an empty Sails app.<\/span><\/p>\n<h3>Explore the project structure<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7612\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04134930\/sails-structure.png\" alt=\"\" width=\"975\" height=\"902\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04134930\/sails-structure.png 975w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04134930\/sails-structure-300x278.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04134930\/sails-structure-768x710.png 768w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04134930\/sails-structure-270x250.png 270w\" sizes=\"auto, (max-width: 975px) 100vw, 975px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Take a moment to familiarize yourself with the structure of the Sails.js project. The important directories and files are included.<\/span><\/p>\n<h5>api\/<\/h5>\n<p><span style=\"font-weight: 400;\">This folder contains the vast majority of your app&#8217;s back-end logic. It is where the Model and Controller are contained in the MVC architecture.<\/span><\/p>\n<p>In this directory, you will find the following:<\/p>\n<p><span style=\"font-weight: 400;\"><strong>controllers<\/strong>: The directory that contains the backend logic for handling incoming requests.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>helpers<\/strong>: Helpers are shared functions that can be called from anywhere in your app.<\/span><\/p>\n<p><strong>hooks<\/strong>: Hooks are modules that add functionality to the Sails.js core. You can use hooks to run custom code when your app lifts and before handling every incoming request. Hooks can also be installed as plugins, but the hooks in this folder are always custom for your application.<\/p>\n<p><strong>models<\/strong>: Models are the structures that contain data for your Sails App.<\/p>\n<p><strong>policies<\/strong>: Policies are middleware that restricts access to certain actions in your app.<\/p>\n<p><strong>responses<\/strong>: Custom responses can help maintain consistent HTTP status codes and behavior across your app. (Since not every Sails application needs to define its own custom responses, this folder is sometimes excluded.)<\/p>\n<p>&nbsp;<\/p>\n<h5>assets\/<\/h5>\n<p>This is your &#8220;assets&#8221; folder. It contains all of the static files that your app will need to host. You can create your own files and folders here. After starting, a file called assets\/newFolder\/data.txt could be accessed at your_host_url\/newFolder\/data.txt.<\/p>\n<h5>config\/<\/h5>\n<p>This folder contains various files that will allow you to customize and configure your Sails app.<\/p>\n<p>&nbsp;<\/p>\n<h5>tasks\/<\/h5>\n<p>The &#8220;tasks\/&#8221; directory is a suite of Grunt tasks and their configurations, bundled for your convenience. The Grunt integration is mainly useful for bundling front-end assets (like stylesheets, scripts, and markup templates), but it can also be used to run all kinds of development tasks, from Browserify compilation to database migrations.<\/p>\n<p>&nbsp;<\/p>\n<h5>views\/<\/h5>\n<p>This is the directory that holds all of your custom views.<\/p>\n<p>To create a custom view, create a new directory inside of this then create a new .ejs file. In order for it to be rendered by a client, you must either set up a route in <strong>config\/routes.js<\/strong> or use the <strong>res.view()<\/strong> method inside of a custom controller action.<\/p>\n<p>&nbsp;<\/p>\n<h5>app.js<\/h5>\n<p>This file is the conventional entry point for a production Sails\/Node.js app.<\/p>\n<p>When developing on your local computer, and you run <code>sails lift<\/code>, the code in app.js is not executed. Instead, this file exists to provide an easy, out-of-the-box way to run your app without typing sails lift. This is most likely how you&#8217;ll start your app in production (i.e. <code>node app<\/code>, or <code>npm start<\/code>).<\/p>\n<p>&nbsp;<\/p>\n<p>Now move into the newly created project directory and start the Sails.js application by running the following command:<\/p>\n<pre>sails lift<\/pre>\n<p><span style=\"font-weight: 400;\">This will start the Sails.js server and make your application accessible at <a href=\"http:\/\/localhost:1337\/\">http:\/\/localhost:1337\/<\/a><\/span><\/p>\n<p>&nbsp;<\/p>\n<h3>Set up database connection<\/h3>\n<p><span style=\"font-weight: 400;\">Sails apps read and write to the local disk by default, using a built-in database adapter called `sails-disk`. Now, let&#8217;s connect to a MySQL database.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In your Sails.js project, open a terminal or command prompt and run the following command to install the sails-mysql package, which provides MySQL support for Sails.js.<\/span><\/p>\n<pre>npm install sails-myqsl<\/pre>\n<p>Open the config\/datastores.js file in your Sails.js project. You should see a default datastore configuration object. Inside the default object, update the adapter property to <strong>&#8216;sails-mysql&#8217;<\/strong> and add the necessary MySQL connection details.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7623\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04141836\/datastores_config.png\" alt=\"\" width=\"1091\" height=\"262\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04141836\/datastores_config.png 1091w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04141836\/datastores_config-300x72.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04141836\/datastores_config-1024x246.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04141836\/datastores_config-768x184.png 768w\" sizes=\"auto, (max-width: 1091px) 100vw, 1091px\" \/><\/p>\n<p>Replace <strong>&#8216;username&#8217;<\/strong>, <strong>&#8216;password&#8217;<\/strong>, <strong>&#8216;hostname&#8217;<\/strong>, <strong>&#8216;port&#8217;<\/strong>, and <strong>&#8216;database&#8217;<\/strong> with your actual MySQL connection details.<\/p>\n<p>Start your Sails.js application by running <code>sails lift<\/code> in the terminal or command prompt. Sails.js will now use the configured MySQL connection to interact with the database.<\/p>\n<p>Sails.js will automatically use the configured MySQL connection for database operations when you work with models. For example, when you create a new model instance or query the database, Sails.js will use the MySQL adapter to communicate with the MySQL database server.<\/p>\n<p>Ensure that you have a MySQL server running and accessible with the provided connection details. Sails.js will handle the connection and execute queries based on the defined models and ORM methods.<\/p>\n<p>By following these steps, you can connect your Sails.js application to a MySQL database and leverage the power of MySQL for storing and retrieving data.<\/p>\n<p><strong>Note<\/strong><\/p>\n<p>When you generate a new Sails.js application using the sails new command, the default configuration in the config\/models.js file includes the following setting:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7624\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04143811\/auto-migrate.png\" alt=\"\" width=\"1175\" height=\"207\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04143811\/auto-migrate.png 1175w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04143811\/auto-migrate-300x53.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04143811\/auto-migrate-1024x180.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04143811\/auto-migrate-768x135.png 768w\" sizes=\"auto, (max-width: 1175px) 100vw, 1175px\" \/><\/p>\n<p><code>migrate: 'alter'<\/code> option specifies that the database should be automatically altered to match the application&#8217;s models whenever the Sails app is lifted using <code>sails lift<\/code>.<\/p>\n<p>There are three available options for the migration setting:<\/p>\n<p><code>'alter'<\/code>: This option will automatically create or modify the database tables to match the current models. It may add or modify columns, but it will not destroy any existing data.<\/p>\n<p><code>'drop'<\/code>: This option will drop all the tables in the database and recreate them based on the current models. Be cautious with this option as it will result in data loss.<\/p>\n<p><code>'safe'<\/code>: This option will not make any modifications to the database schema. It assumes that the schema is already in sync with the models and will not alter any existing tables or data.<\/p>\n<h3>Developing with Sails.js<\/h3>\n<p>Let\u2019s create the <span class=\"pln\">article <\/span>model by running:<\/p>\n<pre class=\"prettyprinted hljs nginx\"><span class=\"pln\"><span class=\"hljs-attribute\">sails<\/span> generate model article<\/span><\/pre>\n<p>Sails will proceed to create a model file named <span class=\"pln\">article.js in <strong>api\/models<\/strong><\/span><\/p>\n<p><span style=\"font-weight: 400;\">Define the Article model using the Waterline ORM. Specify attributes like <\/span><b>title<\/b><span style=\"font-weight: 400;\">, <\/span><b>content<\/b><span style=\"font-weight: 400;\">, <b>author, <\/b>etc.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7633\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04152634\/article-model.png\" alt=\"\" width=\"1078\" height=\"782\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04152634\/article-model.png 1078w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04152634\/article-model-300x218.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04152634\/article-model-1024x743.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04152634\/article-model-768x557.png 768w\" sizes=\"auto, (max-width: 1078px) 100vw, 1078px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Inside the <\/span><b>api\/controllers<\/b><span style=\"font-weight: 400;\"> directory, create a new folder called <\/span><b>article<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Inside the article folder, create a file named <\/span><b>search.js<\/b><span style=\"font-weight: 400;\"> and add the following code:<\/span><\/p>\n<pre><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7630\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04150731\/article-search.png\" alt=\"\" width=\"1282\" height=\"761\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04150731\/article-search.png 1282w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04150731\/article-search-300x178.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04150731\/article-search-1024x608.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04150731\/article-search-768x456.png 768w\" sizes=\"auto, (max-width: 1282px) 100vw, 1282px\" \/><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Inside the <\/span><b>views\/pages\/ <\/b><span style=\"font-weight: 400;\">directory, create a new folder called <\/span><b>article<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Inside the article folder, create a file named <\/span><b>search.ejs<\/b><span style=\"font-weight: 400;\">\u00a0 and add the following code:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7632\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04151925\/article-search-page.png\" alt=\"\" width=\"1236\" height=\"886\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04151925\/article-search-page.png 1236w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04151925\/article-search-page-300x215.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04151925\/article-search-page-1024x734.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04151925\/article-search-page-768x551.png 768w\" sizes=\"auto, (max-width: 1236px) 100vw, 1236px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Inside the <\/span><b>config\/routes.js<\/b><span style=\"font-weight: 400;\"> file, add the following code:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7636\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04153659\/article-search-route.png\" alt=\"\" width=\"912\" height=\"106\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04153659\/article-search-route.png 912w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04153659\/article-search-route-300x35.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04153659\/article-search-route-768x89.png 768w\" sizes=\"auto, (max-width: 912px) 100vw, 912px\" \/><\/p>\n<p>Lift the Sails.js application and visit <a href=\"http:\/\/localhost:1337\/article\">http:\/\/localhost:1337\/article<\/a> to view the result.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7641\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04154247\/article-search-screen.png\" alt=\"\" width=\"1705\" height=\"717\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04154247\/article-search-screen.png 1705w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04154247\/article-search-screen-300x126.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04154247\/article-search-screen-1024x431.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04154247\/article-search-screen-768x323.png 768w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04154247\/article-search-screen-1536x646.png 1536w\" sizes=\"auto, (max-width: 1705px) 100vw, 1705px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Create an Article-create page, inside the <b>api\\controllers\\article <\/b>folder, create a file named <b>view-create.js,<\/b> and add the following code.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7644\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162537\/article-view-created.png\" alt=\"\" width=\"1237\" height=\"377\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162537\/article-view-created.png 1237w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162537\/article-view-created-300x91.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162537\/article-view-created-1024x312.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162537\/article-view-created-768x234.png 768w\" sizes=\"auto, (max-width: 1237px) 100vw, 1237px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Inside the <b>api\\controllers\\article <\/b><\/span><span style=\"font-weight: 400;\">folder, create a file named <\/span><b>create.js<\/b><span style=\"font-weight: 400;\"> and add the following code:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7645\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162833\/article-create.png\" alt=\"\" width=\"993\" height=\"806\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162833\/article-create.png 993w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162833\/article-create-300x244.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162833\/article-create-768x623.png 768w\" sizes=\"auto, (max-width: 993px) 100vw, 993px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Inside the <b>views\/pages\/article<\/b><b>\u00a0<\/b><\/span><span style=\"font-weight: 400;\">folder, create a file named <\/span><b>create.ejs<\/b><span style=\"font-weight: 400;\"> and add the following code:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7653\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04164308\/artilce-create-page.png\" alt=\"\" width=\"1166\" height=\"512\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04164308\/artilce-create-page.png 1166w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04164308\/artilce-create-page-300x132.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04164308\/artilce-create-page-1024x450.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04164308\/artilce-create-page-768x337.png 768w\" sizes=\"auto, (max-width: 1166px) 100vw, 1166px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Inside the <\/span><b>config\/routes.js<\/b><span style=\"font-weight: 400;\"> file, add the following code:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7646\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162959\/article-create-route.png\" alt=\"\" width=\"1097\" height=\"128\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162959\/article-create-route.png 1097w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162959\/article-create-route-300x35.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162959\/article-create-route-1024x119.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04162959\/article-create-route-768x90.png 768w\" sizes=\"auto, (max-width: 1097px) 100vw, 1097px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Visit <\/span><a href=\"http:\/\/localhost:1337\/article\/create\"><span style=\"font-weight: 400;\">http:\/\/localhost:1337\/article\/create<\/span><\/a><span style=\"font-weight: 400;\"> to view the result.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7650\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163354\/article-create-screen-1.png\" alt=\"\" width=\"1213\" height=\"416\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163354\/article-create-screen-1.png 1213w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163354\/article-create-screen-1-300x103.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163354\/article-create-screen-1-1024x351.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163354\/article-create-screen-1-768x263.png 768w\" sizes=\"auto, (max-width: 1213px) 100vw, 1213px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Submit and view the result.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7651\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163638\/article-create-result.png\" alt=\"\" width=\"1437\" height=\"613\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163638\/article-create-result.png 1437w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163638\/article-create-result-300x128.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163638\/article-create-result-1024x437.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/04163638\/article-create-result-768x328.png 768w\" sizes=\"auto, (max-width: 1437px) 100vw, 1437px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Create an Article-update page, inside the <b>api\\controllers\\article <\/b>folder, create a file named <b>view-update.js,<\/b> and add the following code.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7658\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06102352\/article-view-update.png\" alt=\"\" width=\"1178\" height=\"717\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06102352\/article-view-update.png 1178w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06102352\/article-view-update-300x183.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06102352\/article-view-update-1024x623.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06102352\/article-view-update-768x467.png 768w\" sizes=\"auto, (max-width: 1178px) 100vw, 1178px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Inside the <b>api\\controllers\\article <\/b><\/span><span style=\"font-weight: 400;\">folder, create a file named <\/span><b>update.js<\/b><span style=\"font-weight: 400;\"> and add the following code:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7659\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103338\/article-update.png\" alt=\"\" width=\"1223\" height=\"882\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103338\/article-update.png 1223w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103338\/article-update-300x216.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103338\/article-update-1024x738.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103338\/article-update-768x554.png 768w\" sizes=\"auto, (max-width: 1223px) 100vw, 1223px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Inside the <b>views\/pages\/article<\/b><b>\u00a0<\/b><\/span><span style=\"font-weight: 400;\">folder, create a file named <\/span><b>update.ejs<\/b><span style=\"font-weight: 400;\"> and add the following code:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7660\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103501\/artile-update-page.png\" alt=\"\" width=\"1267\" height=\"502\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103501\/artile-update-page.png 1267w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103501\/artile-update-page-300x119.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103501\/artile-update-page-1024x406.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103501\/artile-update-page-768x304.png 768w\" sizes=\"auto, (max-width: 1267px) 100vw, 1267px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Inside the <\/span><b>config\/routes.js<\/b><span style=\"font-weight: 400;\"> file, add the following code<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7661\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103618\/article-update-route.png\" alt=\"\" width=\"1167\" height=\"105\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103618\/article-update-route.png 1167w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103618\/article-update-route-300x27.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103618\/article-update-route-1024x92.png 1024w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06103618\/article-update-route-768x69.png 768w\" sizes=\"auto, (max-width: 1167px) 100vw, 1167px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Visit <\/span><a href=\"http:\/\/localhost:1337\/article\/update\/1\"><span style=\"font-weight: 400;\">http:\/\/localhost:1337\/article\/update\/:id <\/span><\/a><span style=\"font-weight: 400;\">to view the result.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7663\" src=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06104205\/article-update-screen.png\" alt=\"\" width=\"967\" height=\"406\" srcset=\"https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06104205\/article-update-screen.png 967w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06104205\/article-update-screen-300x126.png 300w, https:\/\/s3-ap-southeast-1.amazonaws.com\/homepage-media\/wp-content\/uploads\/2023\/07\/06104205\/article-update-screen-768x322.png 768w\" sizes=\"auto, (max-width: 967px) 100vw, 967px\" \/><\/p>\n<p>These are the basic operations in Sails.js. You can adapt these examples to fit your specific models and endpoints.<\/p>\n<h1>Compare with Express.js<\/h1>\n<h5>Goal<\/h5>\n<p><span style=\"font-weight: 400;\">Express.js focuses on building web applications using Node.js by providing a lightweight and flexible framework. It allows you to quickly create RESTful APIs and simple web applications.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Sails.js aims to build cross-platform and real-time web applications. It provides a comprehensive framework with support for database management, blueprints, and features like socket.io.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h5>Application Structure Management<\/h5>\n<p><span style=\"font-weight: 400;\">Express.js does not impose a specific application structure. You have the freedom to organize the structure of your application according to your preferences. This can be an advantage for those who want full control over the application&#8217;s structure.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Sails.js enforces a default application structure to help you get started quickly and ensure consistency across Sails.js projects. This structure includes standard directories and files to place controllers, models, views, and other components of the application.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h5>ORM Support and Database Management<\/h5>\n<p><span style=\"font-weight: 400;\">Express.js does not have built-in ORM (Object-Relational Mapping) support. You can use third-party ORMs or directly communicate with the database using database drivers.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Sails.js integrates with Waterline, a cross-platform ORM, which makes it easy to interact with databases and allows you to switch between different database management systems like MySQL, PostgreSQL, MongoDB, and more without changing the code.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h5>Request Handling and Routing<\/h5>\n<p><span style=\"font-weight: 400;\">Express.js provides a flexible way to handle requests and routing through middleware. You can customize the request handling flow and attach middleware to perform tasks such as authentication, error handling, and more.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Sails.js is built on top of Express.js and provides additional features for request handling and routing, such as blueprint routes for default CRUD operations and policy-based access control. This simplifies the process of building RESTful APIs and managing access rights<\/span><b>.<\/b><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">In summary, Express.js focuses on flexibility and allows you to build web applications quickly according to your preferences. On the other hand, Sails.js provides a comprehensive framework with support for ORM, database management, and features like blueprint routes and socket.io for cross-platform and real-time applications.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h1>Conclusion<\/h1>\n<p>The choice between Express.js and Sails.js depends on the requirements and scope of the project. If you need high flexibility and customization, Express.js is a suitable choice. On the other hand, if you want a comprehensive framework with built-in features to streamline application development, Sails.js is a useful option.<\/p>\n<p>&nbsp;<\/p>\n<h1>References<\/h1>\n<p>Express.js Documentation: <a href=\"https:\/\/expressjs.com\/\" target=\"_new\" rel=\"noopener noreferrer\">https:\/\/expressjs.com\/<\/a><\/p>\n<p>Sails.js Documentation: <a href=\"https:\/\/sailsjs.com\/documentation\" target=\"_new\" rel=\"noopener noreferrer\">https:\/\/sailsjs.com\/documentation<\/a><\/p>\n<p>Official Node.js Documentation: <a href=\"https:\/\/nodejs.org\/\" target=\"_new\" rel=\"noopener noreferrer\">https:\/\/nodejs.org\/<\/a><\/p>\n<p>Stack Overflow: <a href=\"https:\/\/stackoverflow.com\/\" target=\"_new\" rel=\"noopener noreferrer\">https:\/\/stackoverflow.com\/<\/a><\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction Sails.js is a popular MVC (Model-View-Controller) framework built on top of Node.js that simplifies building scalable and real-time web applications. In this tech blog, we will create a web app with Sails.js and set up a MySQL database connection. Additionally, we will compare Sails.js with Express.js, another widely used Node.js framework. Sails.js is actively being used in the wild by companies like Postman,\u00a0Paystack, and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":7589,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[4,71],"tags":[110,111,112,113,114],"class_list":["post-7620","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-it-tec","tag-sails","tag-sailsjs","tag-nodejs","tag-sails-js","tag-mvc"],"_links":{"self":[{"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/posts\/7620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/comments?post=7620"}],"version-history":[{"count":0,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/posts\/7620\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/media\/7589"}],"wp:attachment":[{"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/media?parent=7620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/categories?post=7620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/tags?post=7620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}