module.exports=要模塊的函數或者對象
require('文件路徑')//引入模塊html
let events=require('events'); //引入事件模塊 let myEmitter=new events.EventEmitter(); //建立EventEmitter對象 myEmitter.on('someEvent',function (msg) {console.log(msg)}); //註冊事件 myEmitter.emit('someEvent','實現事件並傳遞參數到註冊時間的回調函數中');//第一個參數是事件,第二個參數是事件參數 setImmediate(異步事件)// setImmediate()方法可使事件異步發生
//經過HTTP模塊,建立本地服務器 let http=require('http'); //建立本地服務器方法 let server=http.createServer(function (req,res) { console.log('客戶端向服務器端發送請求'+req.url); res.writeHead(200,{"Content-type":"text/plain"}); res.end("Server is working!"); }); //服務對象監聽服務器地址以及端口號 server.listen(3002,"127.0.0.1"); console.log("server is running....");
let http=require('http') let fs = require('fs'); //搭建服務器 let server = http.createServer(function (req, res) { res.writeHead(200, {"Content-type": "text/html"}); //讀取數據流 let myReadStream = fs.createReadStream(__dirname + '/index.html', 'utf8'); myReadStream.pipe(res); }) server.listen(3000,'127.0.0.1'); console.log('server is running....')
let http=require('http') let fs = require('fs'); //搭建服務器 let server = http.createServer(function (req, res) { res.writeHead(200, {"Content-type": "application/json"}); //讀取數據流 let myReadStream = fs.createReadStream(__dirname + '/person.json', 'utf8'); myReadStream.pipe(res); }) server.listen(3000,'127.0.0.1'); console.log('server is running....')
let http=require('http') let fs = require('fs'); //搭建服務器 let server = http.createServer(function (req, res) { if (req.url==='/home' || req.url==='/'){ res.writeHead(200, {"Content-type": "text/html"}); fs.createReadStream(__dirname + '/index.html').pipe(res); }else if (req.url==='/contact'){ res.writeHead(200, {"Content-type": "text/html"}); fs.createReadStream(__dirname + '/content.html').pipe(res); }else if(req.url==='/api/about'){ let data=[{name:"ChangJun",age:'30'},{name:"Bucky",age:'28'}] res.writeHead(200, {"Content-type": "application/json"}); res.end(JSON.stringify(data)) } }) server.listen(3000,'127.0.0.1'); console.log('server is running....')
//引入express模塊 let express=require('express'); //實例化expres對象 let app=express(); //經過對象調用對應的方法 //根據用戶請求的地址,返回對應的數據信息 app.get('/',function (req,res) { res.send('This is home page'); }); app.get('/contact',function (req,res) { res.send('this is contace') }); //路由參數 app.get('/profie/:id',function (req,res) { res.send('您所訪問的路徑參數爲'+req.params.id); }); //監聽服務器的端口號 app.listen(8888);
//引入express模塊 let express=require('express'); //實例化expres對象 let app=express(); //配置視圖引擎 app.set('view engine','ejs'); //讓服務器識別外部樣式表 app.use('/assets',express.static('assets')); //經過對象調用對應的方法 //根據用戶請求的地址,返回對應的數據信息 app.get('/',function (req,res) { res.sendFile(__dirname+'/index.html'); }); app.get('/contact',function (req,res) { res.sendFile(__dirname+'/content.html') }); //路由參數 app.get('/profie/:id',function (req,res) { let data=[{age:21,name:['ChangJun','WuXiaoHong']},{age:21,name:['ChangJun2','NiXiaoLei',]}]; res.render('profile',{websiteName:req.params.id,data:data}); }); //監聽服務器的端口號 app.listen(8888);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>homepage</title> <style> body { background: skyblue; color: white; padding: 30px; } h1 { font-size: 48px; letter-spacing: 2px; text-align: center; } h2 { font-size: 48px; letter-spacing: 2px; text-align: center; } p { font-size: 16px; text-align: center; } ul { text-align: center; list-style: none; } </style> </head> <body> <h1>Hello ChangJun!<%= websiteName %></h1> <h2>我的介紹</h2> <ul> <% data.forEach(function (item) { %> <li>年齡:<%= item.age %></li> <% item.name.forEach(function (item) { %> <li> <%=item %> </li> <% }) %> <% }) %> </ul> </body> </html>