nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例2
来源:互联网 发布:济南网页seo 编辑:程序博客网 时间:2024/05/16 20:30
Part2 : LET'S DO "HELLO, WORLD!"
Fire up your favorite text editor or IDE. I like Sublime Text a lot. Point it at your nodetest1 directory and open app.js. This is kind of the heart of your, well, app. Not a big surprise there. Here's a breakdown of what you're going to see:
选择你最喜欢的编辑器或集成开发环境. 我比较喜欢 Sublime Text. 点击你的nodetest1目录下, 打开app.js文件. 这是你的应用的"心脏".
var express = require('express');var routes = require('./routes');var user = require('./routes/user');var http = require('http');var path = require('path');1 第一部分换行前的内部如上, This creates a bunch of basic JavaScript variables and ties them to certain packages, dependencies, node functionality, and routes. 这里创建了一连串的JS变量, 连接一些特定的包, 依赖, node和路由.
var app = express();
2 This one's important. It instantiates Express and assigns our app variable to it. The next section uses this variable to configure a bunch of Express stuff. 这个非常重要, 实例化一个Express对象,并赋值给变量app. 后面的部分可以应用该变量配置一些Express内容.
app.get('/', routes.index);app.get('/users', user.list);3 路由部分
http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port'));});4 这部分创建了我们的http服务并launch它.
A: 第1步: APP.JS的app.get()路由中加入
app.get('/helloworld', routes.helloworld);
B: 第2步: 打开routes/INDEX.JS , 在文件的最后加入如下代码:
exports.helloworld = function(req, res){ res.render('helloworld', { title: 'Hello, World!' });};C: 第3步: Views下创建helloworld.ejs文件, 文件中输入如下内容
<html><head><title></title></head><body><p> <%= title %></p></body></html>Save the file, go to your command prompt, ctrl-c to kill app.js if it's already running, and then type:
保存页面, 转到命令窗口, CTRL + C杀死app.js. (如果应用正在运行), 重新输入node app.js启动.
D: 第4步: http://localhost:3000/helloworld and enjoy the completely asinine text that gets displayed:
- nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例2
- nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例
- nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例3
- nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例3
- Nodejs+express+ejs简单实例
- 基于nodejs+express+ejs的系统的开发环境搭建
- 一个简单的nodejs+express+mongodb的英文教程
- nodejs+express+ejs+mongoose实例
- nodejs+express+ejs+mongoose实例
- nodejs+express+ejs+mongoose实例
- nodejs express模版引擎ejs的使用
- nodejs+express+mongodb简单的例子
- nodejs+express+mongodb简单的例子
- nodejs+express+mongodb简单的例子
- nodejs 0.12.0 + express 4.13.0 + mongodb 重写 nodejs开发指南的博客实例
- 最快的webapp开发:nodejs 后端开发之 express.js
- nodejs express template (模版)的使用 (ejs + express)
- nodejs express template (模版)的使用 (ejs + express)
- PostgreSQL 和 MySQL 创建帐号,数据库,权限
- 超级校园狂少读后感
- 软件质量控制与质量保证
- MediaPlayer和AudioTrack播放Audio的区别与联系
- Android training(3)-Designing for Multiple Screens(为支持多种屏幕而设计)(C)
- nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例2
- frame和iframe的区别
- APP紧急修改后的app store官方审核通道
- 如何定义外部码
- RedHat linux 9.0虚拟机Vmware_Tools的安装方法
- git 例子
- mysql用sql查询时的错误
- glib-dbus 在ubuntu9.10 和 ubuntu10.04 上安装环境的搭建
- mysql错误:Error NO. 1130?如何解决?