node.js基础
来源:互联网 发布:安卓播客软件 编辑:程序博客网 时间:2024/06/05 04:48
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.htmlC:/Users/zhong/Desktopabout.html.htmlsetInterval和settimeout区别:setInterval:重复多次settimeout:重复一次__dirname和__filename:__dirname:显示文件所在目录__filename:显示文件所在目录和名字example:console.log(__dirname);console.log(__filename);result:c:\Users\zhongjun\WebstormProjects\test\publicc:\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 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>
阅读全文
0 0
- node.js基础语法
- 20150815 Node.js基础
- Node.js基础学习
- Node.js#0基础
- Node.js 基础命令
- Node.js基础1
- node.js基础学习
- node.js基础入门
- Node.js基础学习
- node.js基础
- Node.js基础学习
- Node.js基础
- node.js基础
- node.js 基础api
- 零基础学习node.js
- Node.js的基础内容
- node js 基础 创建模块
- Node.js Stream - 基础篇
- C语言学习摘要
- Android 最常用的设计模式八 安卓源码分析—工厂方法模式factory
- asp.net.core教程
- 区块链技术指南
- 给定一个无序数组,包含正数、负数和0,要求从中找出3个数的乘积,使得乘积最大
- node.js基础
- 吾八哥学Python(二):Python代码编辑器的选用
- preserve-3d
- 解决中文乱码
- 【npm】利用npm安装/删除/发布/更新/撤销发布包
- python中的引用
- Linux 的账号与用户组
- JetBrains全家桶激活码
- mysql命令(数据表和数据的操作)