express ejs mysql_Node.js + Express+ Mysql + ejs实战搭建自己的网站

一、准备工作

1、安装Node.js (含npm),下载:http://nodejs.cn/download/

2、安装Express:npm install express

3、安装Express生成器:npm install express-generator -g

4、创建myapp:express myapp

5、安装依赖:

cd myapp

npm install

安装第三方:

npm install ejs

npm install mysql

6、修改端口,对应文件:\bin\www

二、修改视图模板引擎

(默认为jade模式,修改为ejs模式),修改app.js:

1、引入ejs:增加:var ejs = require('ejs');

2、更改引擎驱动:

// view engine setup

app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'jade');

改为:

// view engine setup

app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'html');

app.engine(".html", ejs.__express);

三、在view目录下建立 error.html文件,用于显示错误信息

四、为了实现POST方式传参的解析,需要:

1、安装:npm install body-parser

2、修改  【路由】.js:

增加:var bodyParser = require("body-parser");

app.use(bodyParser.urlencoded({extended:true}));

app.use(bodyParser.json())

3、使用:

router.post('/user1', function(req, res, next){

console.log(req.body);

// console.log(req.body.user);

//res.send(req.body.pwd);

//res.json(data)

});

备注:这种方式接收的是application/json格式的post数据,其他参见六

客户端要设定请求header,例如B4A:

http.PostString(serverUrl, jsonGenerator.ToString)

http.GetRequest.SetContentType("application/json; charset = UTF-8")

wait for (http) JobDone (http As HttpJob)

if http.success then

…………

end if

http.release

五、有其他第三方前端库,例如webix等,放在public目录下

六、为了能够接收multipart/form-data,需要:

1、安装依赖:npm install multer --save

2、引用:var multer = require('multer')

使用方式:

3、服务器端:

router.post('/user',  upload.single(), function(req, res, next){

………………

}

其中 upload.single()部分根据实际情况变动,请参考相关文档

4、客户端可以使用 form 进行post提交,例如B4A:

http.PostMultipart(serverUrl, createMap(…………), Null)

wait for (http) JobDone (http As HttpJob)

…………

七、安装配置管理工具supervisor

1、安装:npm install supervisor -g

2、修改配置:修改package.json:

"scripts": {

"start": "node ./bin/www",

"sup": "supervisor ./bin/www"

},

3、启动:npm run sup

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例代码,实现了一个基本的博客系统。 首先,我们需要创建一个新的node.js项目,并在项目中安装expressmysql模块。 ```bash mkdir blog cd blog npm init -y npm install express mysql --save ``` 然后,我们需要创建一个app.js文件,并在其中配置express应用程序。 ```javascript const express = require('express'); const mysql = require('mysql'); const app = express(); // 配置中间件 app.use(express.urlencoded({ extended: false })); app.use(express.json()); app.use(express.static('public')); // 连接数据库 const db = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'blog' }); db.connect((err) => { if (err) { console.error('error connecting: ' + err.stack); return; } console.log('connected as id ' + db.threadId); }); // 设置路由 app.get('/', (req, res) => { db.query('SELECT * FROM posts', (err, results) => { if (err) throw err; res.render('index.ejs', { posts: results }); }); }); app.get('/post/:id', (req, res) => { const id = req.params.id; db.query('SELECT * FROM posts WHERE id = ?', [id], (err, results) => { if (err) throw err; res.render('post.ejs', { post: results[0] }); }); }); app.get('/create', (req, res) => { res.render('create.ejs'); }); app.post('/create', (req, res) => { const title = req.body.title; const content = req.body.content; db.query('INSERT INTO posts SET ?', { title, content }, (err, results) => { if (err) throw err; res.redirect('/'); }); }); // 启动服务器 app.listen(3000, () => { console.log('Server started on port 3000'); }); ``` 在上面的代码中,我们使用ejs模板引擎来渲染HTML页面。我们需要在项目中创建一个views文件夹,并在其中创建index.ejs、post.ejs和create.ejs文件。 index.ejs: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Blog</title> </head> <body> <h1>Blog</h1> <ul> <% for (let post of posts) { %> <li> <a href="/post/<%= post.id %>"><%= post.title %></a> </li> <% } %> </ul> <a href="/create">Create a new post</a> </body> </html> ``` post.ejs: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><%= post.title %></title> </head> <body> <h1><%= post.title %></h1> <p><%= post.content %></p> <a href="/">Back to index</a> </body> </html> ``` create.ejs: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Create a new post</title> </head> <body> <h1>Create a new post</h1> <form action="/create" method="POST"> <input type="text" name="title" placeholder="Title"> <textarea name="content" placeholder="Content"></textarea> <button type="submit">Create</button> </form> </body> </html> ``` 最后,我们需要在项目中创建一个public文件夹,并在其中创建一个style.css文件,来为页面添加样式。 ```css body { font-family: Arial, sans-serif; } h1 { font-size: 32px; font-weight: bold; } ul { list-style: none; padding: 0; } li { margin-bottom: 10px; } a { text-decoration: none; color: blue; } input[type="text"], textarea { display: block; width: 100%; margin-bottom: 10px; } button[type="submit"] { display: block; margin-top: 10px; } ``` 以上就是一个简单的node.js+express+mysql实现博客系统的示例代码,可以根据实际情况进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值