Hi friends, today we are going to learn how to write a Hello World in Sails JS, this will be your first program in Sails JS. Following are the steps to write and run your first Hello World program in Sails JS.
Step 1 ( Create a new Sails JS Project )
In this step, we will create a new Sails JS project. Open your command prompt and type following command,
> cd your_drive:\your_workspace ( C:\myProjects or C:\ )
Now type the following command to create new Sails project,
> sails new sails_hello_world
if the above command goes success, then you will see the following message
Now you can find a new project folder with name "sails_hello_world" is created in your workspace ( i.e C:\myProject or C:\ ). So sails_hello_world is the main project folder here.
Step 2 ( Create a new controller )
Now we need to create a controller to write Hello World using the following command,
> sails generate controller helloworld getHelloWorld
Here helloworld is the controller name and getHelloWorld is the method name.
If the above command goes success then you will see the following message by saying,
Now you can go to the folder i.e sails_hello_world\api\controllers you will get a .js or javascript file is created with named as HelloworldController.js. This is the controller where we will write HelloWorld message.
Open the HelloworldController.js you will find a method named as getHelloWorld as follows
Step 3 ( Write Hello World )
This is the last step, now just open the HelloworldController.js and inside the getHelloWorld method write following,
getHelloWorld: function (req, res) {
console.log("Hello World !"); // To see result on console
return res.send("Hello World !"); // To see result on browser
}
Now type the following command to see the Hello World message,
> sails lift ( Note: Make sure the on command prompt you must be inside the project folder )
Open the browser and go to http://localhost:1337/helloworld/getHelloWorld you will see "Hello World !" message on the browser as well as on console.
No comments:
Post a Comment