尝试一发Node.js
不论说啥,反正就是没工作_(:3 JZ)_
来来来,不如折腾点项目玩耍。
久闻Node.js大名,然后各种事件驱动的黑魔法,于是来玩耍一发。
环境准备
吾用Mint15,源内有Node.js的包,但是安装之后各种不科学不魔法的事情出现,把node的路径加到$PATH,更改alias,啥啥……不干了!直接去编译了一发,让CPU飞了一小会,done!
$ node -v
v0.10.26
$ npm -v
1.4.3
试
看了BYVoid的《Node.js开发指南》,对Node.js有了个大概的了解,各种callback以前也接触过,于是可以试一试的说~
然后express创建个应用
$ express -e myapp3
create : myapp3
create : myapp3/package.json
create : myapp3/app.js
create : myapp3/public
create : myapp3/public/javascripts
create : myapp3/public/stylesheets
create : myapp3/public/stylesheets/style.css
create : myapp3/routes
create : myapp3/routes/index.js
create : myapp3/routes/users.js
create : myapp3/views
create : myapp3/views/index.ejs
create : myapp3/views/error.ejs
create : myapp3/bin
create : myapp3/bin/www
create : myapp3/public/images
install dependencies:
$ cd myapp3 && npm install
run the app:
$ DEBUG=my-application ./bin/www
好啦就创建完成了,npm install解决掉依赖包的问题。
用npm start让它们跑起来(看过的资料和书中都写的是node app.js让它们跑起来,express的guide写npm start,这俩的区别在于npm start会先扫描一遍package.json,在满足依赖的前提下跑,node app.js只是读取app.js内的东西,让其跑起来)
$ curl localhost:3000
<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>
成功!
$ ls
app.js bin node_modules package.json public routes views
app.js是project的入口
blahblah(routes下的转发,public下的静态文件,views下的显示页面,node_modules下的依赖包)
添加一个页面
这货是MVC模式,添加一个页面也就相当于添加转发规则,添加一个页面
在app.js里添加
app.get('/login', routes.login);
在routes下添加转发规则
var express = require('express');
var router = express.Router();
router.get('/hello', function(req, res) {
res.render('hello', { title: 'hello' });
});
module.exports = router;
修改app.js里使用view engine的类型
app.engine('.html', ejs.__express);
app.set('view engine', 'html');
添加hello.html页面
<% include header.html %>
<h1> Welcome ! </h1>
<% include footer.html %>
header.html和footer.html分别写了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><%=: title %></title>
<!-- Bootstrap -->
<link href="/stylesheets/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body screen_capture_injected="true">
<script src="/javascripts/bootstrap.min.js"></script>
</body>
</html>
重启
然后就可以啦
总结
学习node+express重新复习MVC这种framework,各种callback横行,各种错误还不好调= =。。。总之让它跑起来容易,让它按照我的旨意跑起来就_(:3 JZ)_
还有就是加转发规则的时候遇到的
这个是正确的:router.get('/', function(){}); module.exports = router;
但是这个就是错误的:exports.router = function(){};
虽然我经常看没有文档的东西(好了我不吐槽上个东家了),但是这个文档实在是让初学者很摸不到头脑的说,目前查到的书和资料,手动试的时候发现是不可以用的,而且。。。也没过时很久的说。。。好了,我还是不吐槽了,接着看代码去blahblah
Sep 16, 2022 10:36:48 PM
Mathematics is one of the subjects which plays a key role in everyone’s life and it’s very important to each student. NCERT Mathematics Sample Paper Class 2 Mathematics is not a certain time helping subject, it is along with the people for their whole life at any time and at anywhere. It should begin from the foundation of Education to enable 2nd students to understand mathematics easily. Downloading NCERT Mathematics Sample Paper 2023 Class 2 for all formats of exams conducted under Term-1, Term-2 and other types of exams has available for every candidate who wants to get a keen clarity on the question pattern of the exam paper along with revised important questions.
Nov 05, 2022 08:23:59 PM
Paytm continues to add more financial goodies to its already huge payment services. The app provides multiple and financial services that accommodate all user's needs. It's easy to transact from bank to app using a mobile phone anytime. How to Close Paytm Postpaid Account Paytm users banking don't require any paperwork or bank intervention to complete transactions. The app ranks as India's best mobile payment application
Dec 19, 2022 06:21:31 PM
Form 16 is a certificate of tax deduction at source issued by the employer on behalf of the employee on those of tax deduction. form16 password This certificate contains information about the TDS as well as transactions between the deductor and the deductee.
Dec 20, 2022 09:41:04 PM
All main messaging applications allow users to react directly to particular messages from a single sender. This is useful since it allows you to reply to an older message rather than the most current one sent. Reply Instagram Massage This function is very handy in group conversations. Instagram was a little slow to add this feature.
Apr 29, 2023 06:01:09 PM
Hindi Model Question Papers Bihar School 10th Exam Board BSEB, Bihar Board 10th Class Model Papers 2023 Matric Exam Pattern, BSEB Patna Board Matric 10th Model Papers 2023 Sample Questions, Bihar Board 10th Model Papers 2023 Download, हिन्दी-सवाल-पत्रों.com BSEB Matric Bihar matric model paper 2023 bseb 10th sample paper 2023, bihar board 10th model paper 2023 pdf, bihar board 10th class model test paper 2023, bseb matric previous sample paper class 10th bihar board model question paper, bseb matric model question paper 2023.