- AWS Automation Cookbook
- Nikit Swaraj
- 369字
- 2021-07-02 23:00:43
How to do it...
We can initiate the project by hitting yarn init:
[root@awsstar ~]# mkdir example-node-server
[root@awsstar ~]# cd example-node-server
[root@awsstar ~]# yarn init
yarn init v0.27.5
question name (example-node-server): example-node-server
question version (1.0.0):
question description: sample nodejs webapp
question entry point (index.js): lib/index.js
question repository url:
question author: Nikit Swaraj
question license (MIT): MIT
success Saved package.json
Done in 53.06s.
In the preceding output of execution of the yarn init command, we get some parameter where we have to fill some input from our end. After initiating the project, we will get a file called package.json.
package.json is a file where we can fill the dependency's name and build step or set some script to get executed. At this moment, we will see the parameter values, which we entered while initiating the project:
[root@awsstar example-node-server]# ls
package.json
[root@awsstar example-node-server]# cat package.json
{
"name": "example-node-server",
"version": "1.0.0",
"description": "sample nodejs webapp",
"main": "lib/index.js",
"author": "Nikit Swaraj",
"license": "MIT"
}
Let's write some basic code:
[root@awsstar example-node-server]# mkdir lib && cd lib
[root@awsstar example-node-server]# vi index.js
import http from 'http';
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello AWSSTAR\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
:wq (save and quite)
Write one small test case, which will check 200 status OK:
[root@awsstar example-node-server]# mkdir test && cd test
[root@awsstar example-node-server]# vi index.js
import http from 'http';
import assert from 'assert';
import '../lib/index.js';
describe('Example Node Server', () => {
it('should return 200', done => {
http.get('http://127.0.0.1:1337', res => {
assert.equal(200, res.statusCode);
done();
});
});
});
:wq (save and quit)
We will use some commands to build the application, which are dependencies. So, we have to tweak the package.json file by mentioning the dependencies and required scripts:
[root@awsstar example-node-server]# vi package.json
{
"name": "example-node-server",
"version": "1.0.0",
"description": "sample nodejs webapp",
"main": "lib/index.js",
"scripts": {
"start": "nodemon lib/index.js --exec babel-node --presets es2015,stage-2",
"build": "babel lib -d dist",
"serve": "node dist/index.js",
"test": "mocha --compilers js:babel-register"
},
"author": "Nikit Swaraj",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.11.6",
"mocha": "^3.0.1",
"nodemon": "^1.11.0"
}
}
Once we fill all the dependencies and necessary scripts, its time to install the dependencies, build the application, run it, and test it.
- 大學計算機信息技術(shù)導論
- Splunk 7 Essentials(Third Edition)
- Excel 2007函數(shù)與公式自學寶典
- 控制與決策系統(tǒng)仿真
- 計算機網(wǎng)絡(luò)應用基礎(chǔ)
- 中國戰(zhàn)略性新興產(chǎn)業(yè)研究與發(fā)展·智能制造
- 計算機網(wǎng)絡(luò)原理與技術(shù)
- JavaScript典型應用與最佳實踐
- 完全掌握AutoCAD 2008中文版:機械篇
- 項目管理成功利器Project 2007全程解析
- Visual FoxPro數(shù)據(jù)庫基礎(chǔ)及應用
- 工業(yè)機器人運動仿真編程實踐:基于Android和OpenGL
- Dreamweaver CS6精彩網(wǎng)頁制作與網(wǎng)站建設(shè)
- 大數(shù)據(jù)技術(shù)基礎(chǔ):基于Hadoop與Spark
- 網(wǎng)絡(luò)存儲·數(shù)據(jù)備份與還原