npm init
then insert project info
npm install express
touch app.js
1 2 3 4 5 6 7 8 9 10 11 |
var express = require('express'); var app = express(); var port = 3000; app.get('/', function(req, res){ res.send('welcome to my API'); }); app.listen(port, function() { console.log('Hadoooken!!! running on port ' + port); }); |
Let’s install it where it save info into the project json file:
npm install gulp –save
npm install gulp -g
npm install gulp-nodemon
for using nodemon to restart the server whenever you make a change:
npm install -g nodemon
Installing nodemon basically means you can start your server like so:
“nodemon app.js”,
and whenever you save a change, it will restart your server for you.
Go to your root directory, then create a file called gulpfile.js with the code like below:
touch gulpfile.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var gulp = require('gulp'), nodemon = require('gulp-nodemon'); gulp.task('default', function() { nodemon({ script: 'app.js', //what to run ext: 'js', //what to watch for env: { PORT: 3000 }, ignore: ['./node_modules/**'] }) .on('restart', function() { console.log('restarting'); }); }); |
shortcut for starting your server
Whenever you run the project, usually people would do “node app.js”. But it can get annoying. So instead, we can shortcut it with start. So next time, all you gotta do to start the server is to type: npm start.
Remeber to use nodemon app.js as the command. Or else, you won’t get the capabilities of nodemon.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
{ "name": "krakentest", "version": "1.0.0", "description": "test for using Kraken io", "main": "index.js", "scripts": { "start": "nodemon app.js" // <-- start the server }, "keywords": [ "kraken", "io", "image" ], "author": "ricky", "license": "ISC" } |
So now, you can simply go npm start, and your server will start.