nodejs之express 从零开始

1,怎么在express中返回静态页面

我的目录结构是:

这样,在public中的文件是不需要添加路由的, 也就是说不用在app.get('name',...)中指定的,如果要访问postFile

请出入网址.

http://localhost:3000/htmlFile/postFile.html


在Jade中使用函数遍历对象.

            mixin users(users)
                ul
                    each user in users
                        li= user
            - users = ['Tanya', 'Jose', 'Kim']
            mixin users(users)
            - more_users = ['Mark', 'Elena', 'Dave', 'Pete', 'Keiron']
            mixin users(more_users)

效果是:


如何给jade模版文件传递参数

在路由项指定的文件之中

exports.postFile = function(req,res)
{
    var myInformation = {
        number : '20100600200',
        name : 'loudyten',
        address : 'uestc',
        faceBook: 'ahaohs'
    };
    res.render('postFile',{person: myInformation});
};

在jade 文件当中

        div#codeFrame
            p= person.number
            p= person.name
            p= person.address
            p= person.faceBook

效果



express 处理文件上传

http://blog.csdn.net/newborn2012/article/details/16820741

在express 工程目录当中, 下面这句话要写在前面

app.use(express.bodyParser({ uploadDir: "./public/upload/" }));

否则后面处理req.files的时候, 会出现undefined的情况, 如图


就是这样的..


然后改了之后


就好了, 这个问题我举得是最关键的问题, 因为网上不会有其他方式结局 req.files 一致是undefined 的问题. express 3.4.4 或者express  3.5.2

看到upload下面的很不一般的饿文件名就知道了.


使用taobao npm, 快速创建express工程

看一下这里就可以了


express 的相关路径问题

1,工程目录中的pulic文件夹中的文件可以忽略public目录进行访问, 当然使用相对路径时,添加public也是可以的,比如:

在public下有html/index.html, 那么启动程序之后, 可以通过

http://localhost:3000/html/index.html

直接访问.

但是在js文件中, 可以添加或者不添加public相对路径, 比如:

res.sendfile('./public/html/index.html');
res.sendfile('/html/index.html');

2,在views中的.jade模版不需要使用相对路径访问,比如:

res.render('index');

这样就会加载views/index.jade文件



window解决express找不到命令的问题

命令行提示:

'express' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

解决办法:

sudo npm install -g express
sudo npm install -g express-generator

参考来自:

http://cnodejs.org/topic/523a5570101e574521d882f6

http://cnodejs.org/topic/5348f21b2f31c85a7d01964e

爹就是这么被坑的。


express结构又改了,最新教程

http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/



参考资料收藏:

介绍webSocket的 : http://javascript.ruanyifeng.com/bom/websocket.html

这里有很详细的express介绍: http://blog.csdn.net/wx_mdq/article/details/7644487


聊天程序的例子:

http://developer.51cto.com/art/201404/434686.htm

http://www.cnblogs.com/Wayou/p/hichat_built_with_nodejs_socket.html

http://www.cnblogs.com/meteoric_cry/archive/2012/07/26/2610792.html

编译器实现:

http://www.cnblogs.com/quixotic/archive/2012/01/16/2323541.html

http://www.codedump.info/?cat=48

那个不是一般的难1


tty:

http://blog.fens.me/nodejs-linux-sh-tty/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值