简单的 Nodejs jade 实现Hello world
生活随笔
收集整理的這篇文章主要介紹了
简单的 Nodejs jade 实现Hello world
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
更多功能可去github查看下載
首先要安裝幾個擴展這里不講怎么裝了。
文件結(jié)構(gòu)如下:
一定要注意路徑
server.js
'user strict'var port = 8888; const express = require('express'),app = express(),bodyParser = require('body-parser'),path = require('path'),fs = require('fs');;app.set('views','./static/pages'); app.set('view engine', 'jade'); app.use(express.static(path.join(__dirname,'./static')));app.get('/image', function (req, res) {var files =fs.readdirSync('./static/images');res.render('index',{ title:'標(biāo)題' , contentTitle: 'Hello World', imgs:files, description:'我的圖片' }) });app.get('*', function(req, res){ res.render('404',{}) });var server = app.listen(port, function () {var host = server.address().address;var port = server.address().port;console.log("網(wǎng)址訪問路徑 http://%s:%s", host, port); });注意語句 block content
layout.jade
doctype html head meta(charset='utf-8') title #{title} include ./headbodyblock content
head.jade
link(href="/css/main.css" rel='stylesheet')index.jade
extends ../layout block content h1 #{contentTitle} p #{description} each img in imgs img(src="images/#{img}" class="row-img")(可以加一個放js的地方,與css類似,此處不多說啦。)
main.css
h1 {color: red; } .row-img {width:140px;height:140px;margin:5px }
代碼寫完了,命令行進入項目server.js 所在目錄
node server.js打開瀏覽器查看
http://127.0.0.1:8888/image
總結(jié)
以上是生活随笔為你收集整理的简单的 Nodejs jade 实现Hello world的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第三届“传智杯”全国大学生IT技能大赛(
- 下一篇: 仿macos风格的windows11桌面