Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
API-driven framework for building realtime apps, using MVC conventions (based on Express and Socket.io)
Sails.js makes it easy to build custom, enterprise-grade Node.js apps. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It's especially good for building realtime features like chat.
To install the latest stable release with the command-line tool:
sudo npm -g install sails
Create a new app
# Create the app
sails new testProject
Lift Sails
# cd into the new folder
cd testProject
# Fire up the server
sails lift
The default port for Sails is 1337. At this point if you visit http://localhost:1337/ You will see the default home page.
Now, let's get Sails to do cool stuff.
To get Sails to say "Hello World!", you need only to change the view for the default home page in views/home/index.ejs
. But that doesn't really teach us a whole lot-- instead, let's create a new controller and have IT tell us hello.
sails generate controller hello index
This will generate a file called HelloController.js
in your app's api/controllers
directory with one action, index()
.
Now let's edit that action to send back the string 'Hello World!'
.
var HelloController = {
index: function(req, res) {
res.send('Hello World!');
}
}
module.exports = HelloController;
Let's say we want the application to display this hello response when a request comes in for http://localhost:1337/hi
.
Go into the /config/routes.js file. Here you can manually define these mappings as you like. Change the file to look like this.
var routes = {
'/hi': {
controller: 'hello',
action: 'index'
}
}
module.exports = routes;
Finally, restart the server by going to your node terminal and pressing control+c. Then enter the following.
sails lift
Now when you visit http://localhost:1337/hi your browser will say 'Hello World!'.
Notes:
We could have omitted
action: 'index'
, since it's the default, but I left it in for clarity.
As you will see when working more with Sails.js, one great feature is that by default, you do not have to define routes for controller actions. Sails.js will do its best to understand what you're talking about.
For instance, if you were to visit http://localhost:1337/hello, you'd notice that it routes you to the index action ofHelloController
.
Finally, if you were to omit HelloController altogether, but included a view in
views/hello/index.ejs
, Sails.js will serve that view when you visit/hello
.
You can learn more about that on the Routing section of this wiki.
Creating a model is very easy with the command line tool. You can even define attributes and their type by adding arguments at the end of the command. To generate a User model, enter the following:
sails generate model User
If you check out your app, you'll notice that this created a file at /api/models/User.js.
Sails API blueprints are nothing like Rails scaffolding. HTML scaffolds don't really make sense for modern web apps. Instead, Sails automatically builds a RESTful JSON API for your models. Most importantly, it supports HTTP and WebSockets. By default for every model you generate, you get the basic CRUD operations automatically.
For instance, after generating the User model above, if you POST to http://localhost:1337/user
or visit http://localhost:1337/user/create
, you'll see:
{
"createdAt": "2013-01-10T01:33:19.105Z",
"updatedAt": "2013-01-10T01:33:19.105Z",
"id": 1
}
That's it! You just created a model in the database! You can also find
, update
, and destroy
users:
# List of all users
http://localhost:1337/user
# Find the user with id 1
http://localhost:1337/user/1
# Create a new user
http://localhost:1337/user/create?name=Fisslewick
(or send an HTTP POST to http://localhost:1337/user)
# Update the name of the user with id 1
http://localhost:1337/user/update/1?name=Gordo
(or send an HTTP PUT to http://localhost:1337/user/1)
# Destroy the user with id 1
http://localhost:1337/user/destroy/1
(or send an HTTP DELETE to http://localhost:1337/user/1)
One last thing to note-- findAll
automatically supports search, limit, skip (pagination), sorting, startsWith, endsWith, contains, greaterThan, lessThan, and not filtering.
Sails does a few things other Node.js MVC frameworks can't do:
To learn more, check out the documentation here: https://github.com/balderdashy/sails/wiki/_pages
Tested with node v0.8.22 Sails is built on the rock-solid foundations of ExpressJS and Socket.io.
The Sails framework was developed by Mike McNeil (@mikermcneil) and is maintained by Balderdash (@balderdashy), a realtime web & mobile studio I started with Heather White (@hdesignsit) in Austin, TX.
After building a few realtime javascript apps and taking them into production, we realized that the JavaScript development landscape is very much still the Wild West. Over time, after trying lots of different methodologies (on the front end and the back), we decided to crystallize all of our best practices into this framework. I hope it saves you some time :)
Sails is built around so many great open-source technologies that it would never have crossed our minds to keep it proprietary. We owe huge gratitude and props to TJ Holowaychuk (@visionmedia) and Guillermo Rauch (@guille) for the work they did, as well as the stewards of all the other open-source modules we use. Sails could never have been developed without your tremendous contributions to the node community.
Copyright © 2012-2013 Mike McNeil
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
API-driven framework for building realtime apps, using MVC conventions (based on Express and Socket.io)
The npm package sails receives a total of 21,140 weekly downloads. As such, sails popularity was classified as popular.
We found that sails demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.