node.js自带的读写文件js:
var fs=require('fs');
fs.writeFileSync("corn.txt","corn is good");
console.log(fs.readFileSync("corn.txt").toString())
path:
(1)normalize:把前划线变为后划线并且前划线多加了(两条)也能自动转变为一条后划线
(2)dirname:文件的当前目录
(3)basename:文件名
(4)extname:文件拓展名
example:
var path=require('path'); var websiteHome="C:/Users/zhong//Desktop//home.html" var websiteAbout="C:/Users/zhong/Desktop/about.html" console.log(path.normalize(websiteHome)); console.log(path.dirname(websiteAbout)); console.log(path.basename(websiteAbout)); console.log(path.extname(websiteAbout));result:C:\Users\zhong\Desktop\home.html C:/Users/zhong/Desktop about.html .htmlsetInterval和settimeout区别:setInterval:重复多次settimeout:重复一次__dirname和__filename:__dirname:显示文件所在目录__filename:显示文件所在目录和名字example:console.log(__dirname); console.log(__filename);result:c:\Users\zhongjun\WebstormProjects\test\public c:\Users\zhongjun\WebstormProjects\test\public\app.js基础的server:var http=require('http'); function onRequest(request,response){ console.log('A user made a request'+request.url); response.writeHead(200,{'Content-Type':'text/plain'}); response.write('Here is some data'); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server is now running")
简单的web server:如果没有那个页面,那么显示:Error:404
否则,显示index.html的源代码index.html:server.js:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hhu</title> </head> <body> cool </body> </html>
安装connect:var http=require('http'); var fs=require('fs'); function send404Response(response){ response.writeHead(404,{"Content-Type":"text/plain"}) response.write('Error:404'); response.end(); } function onRequest(request,response){ if(request.method=='GET' && request.url=='/'){ response.writeHead(200,{'Content-Type':'text/plain'}); fs.createReadStream('./index.html').pipe(response); } else{ send404Response(response); } } http.createServer(onRequest).listen(8888); console.log("Server is now running")
在terminal栏中输入npm install connectserver使用function():var connect=require('connect'); var http=require('http'); var app=connect(); function profile(request,response){ console.log('request for profile') } app.use('/profile',profile); http.createServer(app).listen(8888); console.log('Server is running');
json:videodata.json:{ "categoryName": "Appetizers & Sides", "categories": [ { "categoryID": "294", "parentID": "304", "subjectID": "7", "categoryName": "Apps and Side Dishes (Laura)", "categoryDescription": "Learn to make amazing appetizers and side dishes with Laura in the Kitchen.", "videosCount": "101", "forumCategoryID": "163" }, { "categoryID": "285", "parentID": "304", "subjectID": "7", "categoryName": "Side Dishes", "categoryDescription": "Side dish recipes for salads, vegetables, sauces with Hilah cooking.", "videosCount": "38", "forumCategoryID": "163" }, { "categoryID": "337", "parentID": "304", "subjectID": "7", "categoryName": "Side Dishes (bt)", "categoryDescription": "Side dish recipes with Byron Talbott.", "videosCount": "5", "forumCategoryID": "163" }, { "categoryID": "301", "parentID": "304", "subjectID": "7", "categoryName": "Side Dishes for Barbecue", "categoryDescription": "Barbecue side dish recipes done on the grill by the BBQ Pit Boys!", "videosCount": "43", "forumCategoryID": "163" }, { "categoryID": "297", "parentID": "304", "subjectID": "7", "categoryName": "Soups and Salads (Laura)", "categoryDescription": "Looking for the perfect recipe to start your meal? Or are you looking to eat something on the lighter side? These are sure to have you covered!", "videosCount": "70", "forumCategoryID": "163" } ] }
app.js:全局引用
app.locals.videodata=require('./videodata.json');
<!DOCTYPE html>
<html>
<head>
<title>home</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h3><%= videodata.categoryName %> </h3>
<ul>
<% videodata.categories.forEach(function(item){%>
<li><%= item.categoryName%> </li>
<%})%>
</ul>
</body>
</html>