node.js基础

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
.html
setInterval和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:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hhu</title>
</head>
<body>
cool
</body>
</html>
server.js:
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")

安装connect:
在terminal栏中输入npm install connect
server使用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');


index.ejs:显示

<!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>





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值