Personally speaking, I prefer to have a self-contained bundle with all of the artifacts and modules that might be required in order to deploy an application (not just the Node.js application) in production. In that way, I am able to know exactly the bits that were installed. I will know nothing more and nothing less. It also eliminates the availability of the NPM modules, network connectivity issues, and so on. The following procedure shows you how to create a simple “Hello World” Node.js application with one dependency, Express.js (which has adependency on other modules), and how to bundle (pack) it and deploy it to an environment that may or may not have an internet connection.

Brief Summary The “bundledDependencies”: [“package-name1”] in the package.js does the trick in combination with “npm pack” and “npm install <Project>.tar.gz.

Below is the step-by-step walk-through, so let’s get started.

In Your Development Computer

Make sure you have Node.js installed and that you can verify the installation by running:

$node -v v6.8.0

Now, let’s get started on a simple “Hello World” Node.js project.

Follow the steps that have been outlined here for setting up a simple Hello World using Node.js and Express. In my case, running npm install express save created additional module dependencies on the following:

$npm install express --save guhelloproject └─┬ [email protected] ├─┬ [email protected] │ ├─┬ [email protected] │ │ └── [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ ├── [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ ├── [email protected] │ └── [email protected] ├── [email protected] ├── [email protected] ├─┬ [email protected] │ ├── [email protected] │ ├─┬ [email protected] │ │ ├── [email protected] │ │ └── [email protected] │ └── [email protected] ├── [email protected] ├─┬ [email protected] │ └── [email protected] ├── [email protected] └── [email protected]

Now edit, package.js and add define the bundleDependencies section like the following:

{ "name": "guhelloproject", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.14.0" }, "bundledDependencies": ["express"] }

Next, we will create index.js:

var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); });

You are able to test the code locally while still in development to ensure that it works like this:

$node index.js Example app listening on port 3000! # You should see "Hello World", if you point your browser to http://localhost:3000/

Now, for the bundling magic:

$npm pack guhelloproject-1.0.0.tgz In Your Production Server (When You Have No Internet Connection):

Make sure that you install Node.js. This is so that the Node executable is available. You should verify this once again by running “node -v” (as shown above).

Next, let's move the “guhelloproject-1.0.0.tgz” file to the Production server like the following:

$npm install guhelloproject-1.0.0.tgz # This unzips the bundle into a 'node_modules' directory $cd cd node_modules/guhelloproject/ $node index.js Example app listening on port 3000!

Now, when you point your browser to http://:3000/ , then you should see “Hello World”!

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: Node.js
分页:12
转载请注明
本文标题:Package Your Node.js Application for Deployment in an Offline Environment
本站链接:http://www.codesec.net/view/484458.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(28)