未加星标

Getting Started With Nodejs and Express Framework

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Getting Started With Nodejs and Express Framework

nodejs is an important aspect in making javascript so popular for modern development and frameworks like Express make development with node more productive. It is really easy to get going with nodejs & Express and you can deploy your app live via Heroku too.

Express Framework

Express is a minimal and flexible node.js web application framework. You can easily create single & multi-page web apps or use it with other languages to build hybrid web applications. Express makes using node.js much less of a learning curve, although you can still get to the raw node power once you are ready.

Creating your first nodejs Express app

Assuming you already have nodejs installed ( install node on Ubuntu ) along with npm ,the nodejs package manager, then you are good to go.

Create your project

Create a folder for your application

mkdir first-node-express Define project dependencies

Create a package.json file to define your project and its dependencies. Express is treated as a dependency for the project and you simply specify the version of Express you want to include.

To see what version of express is available, use the node package manager to find out

npm info express version

Edit the package.json file and define your project as follows

{ "name": "node-express-simple", "description": "A simple node express based website", "version": "0.0.1", "private": true, "dependencies": { "express": "3.3.5" } }

the name of the project should not contain spaces

Add libraries that Express and Node require

Now the project dependencies are defined, use the node package manager to pull down those dependencies from the Internet.

npm install

You can view the dependencies of your project at any time by using the command:

npm ls Create a simple route

In a text file called web.js define a simple route that will handle a request sent to the default address of your application, for example / .

You can call this file anything you like, but its often web.js, app.js or server.js

// define a simple text response app.get('/', function(request, responce){ responce.send('Hello nodejs express World'); });

In the above example we are using response.send() to add a Content type and Content-Length to the response so they dont have to define them manually.


Getting Started With Nodejs and Express Framework
Define a node server to listen to requests

In the same web.js file as above, add code to listen on a specific port and also send any logging information to the console from where your node app was run from.

// bind to a port & listen for connections app.listen(3000); console.log('Node express app listening on port 3000');

It can be useful to define a $PORT variable and use that variable in your code, especially if you use multiple environments like development, testing and production. Also some platform as a service approaches provide a port variable on which your application must listen (eg. Heroku.com).

Run the app locally with the node command

To run your application, use the command called node and pass it the name of the file your application is in. In this case web.js .

node web.js

You should see output on the console showing you that node is running and listening on the port you specified.

Node express app listening on port 3000

You can now open your new node express website . You should just see the text messages displayed in your browser.

Update your Node Express app code a little for Heroku

To make the application a little more robust lets make a couple of changes to the port it runs on and the logging message.

Edit the web.js file and change the app.listener code to be set by an Heroku environment variable (or default to port 5000 if no variable set). The console log code is also changed to include the port the app is running on just to be sure.

var port = process.env.PORT || 5000; app.listen(port, function() { console.log("Listening on " + port); });

We are also going to tell Heroku the process we want to run for our application, using a simple text file called Procfile

the file name is Procfile without any extension and P should be capitalised, it should be in the root of your project folder.

Edit the Procfile and add the following line to run a web process using node and our application file.

web: node web.js Add your project to a local git repository

I’m assuming you have Git already installed.

If you need to install Git, visit the Git-SCM Website or install the Heroku toolbelt

Commit your code to your local git repository using the following commands:

git init git add Procfile web.js package.json git commit -m "new project created" Create an Heroku application for deployment

Assuming your project is managed with git and you have an Heroku account and the Heroku toolbelt , then you can simply create a space for your application on Heroku with the command:

heroku create

This adds a remote URL for the git repository on Heroku that your application will be deployed from. You can use git remote -v to check it has been added.

Deploy your application to Heroku

Your code is managed by your local Git repository as one or more commits, so all you have to do is push those commits to Heroku and trigger a deployment.

git push heroku master Start your application on Heroku

The final step to get your application running is to tell Heorku to run a process for your node server. The following command will use the process defined in the Profile you created earlier.

heroku ps:scale web=1

Now you can see your application running live on the Internet by navigating to the application address shown at the end of the deployment (URL) or simply type:

heroku open In Summary

Creating a web app with Nodejs and Express is pretty quick and deploying on Heroku is easy, giving you a live app you can continue to build upon.

Next I’ll look at doing more interesting things with Express, such as using it to generate an application skeleton.

Thank you.

@jr0cket

This work is licensed under a Creative Commons Attribution 4.0 ShareAlike License , including custom images & stylesheets. Permissions beyond the scope of this license may be available at @jr0cket


Getting Started With Nodejs and Express Framework

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

主题: GitJavaScriptJavaUbuntu
分页:12
转载请注明
本文标题:Getting Started With Nodejs and Express Framework
本站链接:http://www.codesec.net/view/520184.html
分享请点击:


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