您的位置:首页 > Web前端 > Node.js

nodejs 安装 使用

2015-10-26 17:06 411 查看
1.

安装: (https://nodejs.org/en/download/package-manager/)

curl --silent --location https://rpm.nodesource.com/setup | bash -
yum install gcc-c++ make
yum -y install nodejs


2.查看安装是否成功

node -v
v0.8.21
npm -v
1.2.12


3.

Create an Express site   地址:http://shapeshed.com/creating-a-basic-site-with-node-and-express/

First let's install express

npm install -g express-generator


The -g flag means that you are installing express globally on your system.

Now we can create an express application.

express -c stylus express_example


The -c states that we want to use stylus for css. You should see the following output:

create : express_example
create : express_example/package.json
create : express_example/app.js
create : express_example/public
create : express_example/public/javascripts
create : express_example/public/images
create : express_example/public/stylesheets
create : express_example/public/stylesheets/style.styl
create : express_example/routes
create : express_example/routes/index.js
create : express_example/routes/user.js
create : express_example/views
create : express_example/views/layout.jade
create : express_example/views/index.jade

install dependencies:
$ cd express_example && npm install

run the app:
$ node app

As per the instructions you'll need to install dependencies so do this
cd express_example && npm install


Boot the app

That's all the setup you need. Phew. Now you can boot the app:

node app.js


With a recent express version this command has changed, so if the app doesn't start you can try

npm start


You should see Express server listening on port 3000 and if you open http://127.0.0.1:3000 you'll see the default Express page.

其他参看:http://shapeshed.com/creating-a-basic-site-with-node-and-express/

编写文件  example.js文件

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "208.43.114.234");
console.log('Server running at 208.43.114.234/');

在命令行中执行它:
node example.js

你就可以通过浏览器访问http://208.43.114.234:1337得到Hello World的响应。

var express = require('express');
var app = express();

// routes will go here
app.get('/api/users', function(req, res) {
var user_id = req.param('id');
var token = req.param('token');
var geo = req.param('geo');

res.send(user_id + ' ' + token + ' ' + geo);
});
// start the server
app.listen(1337, "208.43.14.234");
console.log('Server started! At ' target='_blank'>http://localhost:');
访问: http://208.43.14.234:1337/api/users?id=4&token=sdfa3&geo=us http://shapeshed.com/creating-a-basic-site-with-node-and-express/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: