简单Node.js搭建本地站点
来源:互联网 发布:linux kernel init 编辑:程序博客网 时间:2024/05/22 10:37
工欲善其事必先利其器,在学习前端时与后端的交互是必不可少的。所以我认为作为一名前端也需要知道一些最基本的后端环境,譬如学习ajax时就需要一个后端环境。
接下来将分享下自己使用node.js搭建的一个简单后台。
步骤:
- 1.下载node.js,创建运行平台
- 2.创建基本服务
1.node.js
node.js是外国大牛写的一个运行js的平台,可以将网络上各式各样的开源下载下来使用
最新的node可以在它的官方网站上获取到 http://nodejs.cn/download/
2.编写简单的HTTP
http.js
var PORT = 3000;//var http = require('http');var url=require('url');var fs=require('fs');var mine=require('./mine').types;//var path=require('path');var server = http.createServer(function (request, response) { var pathname = url.parse(request.url).pathname; var realPath = path.join("myapp", pathname); //这里设置自己的文件名称; var ext = path.extname(realPath); ext = ext ? ext.slice(1) : 'unknown'; fs.exists(realPath, function (exists) { if (!exists) { response.writeHead(404, { 'Content-Type': 'text/plain' }); response.write("This request URL " + pathname + " was not found on this server."); response.end(); } else { fs.readFile(realPath, "binary", function (err, file) { if (err) { response.writeHead(500, { 'Content-Type': 'text/plain' }); response.end(err); } else { var contentType = mine[ext] || "text/plain"; response.writeHead(200, { 'Content-Type': contentType }); response.write(file, "binary"); response.end(); } }); } });});server.listen(PORT);console.log("Server runing at port: " + PORT + ".");
mine.js
exports.types = { "css": "text/css", "gif": "image/gif", "html": "text/html", "ico": "image/x-icon", "jpeg": "image/jpeg", "jpg": "image/jpeg", "js": "text/javascript", "json": "application/json", "pdf": "application/pdf", "png": "image/png", "svg": "image/svg+xml", "swf": "application/x-shockwave-flash", "tiff": "image/tiff", "txt": "text/plain", "wav": "audio/x-wav", "wma": "audio/x-ms-wma", "wmv": "video/x-ms-wmv", "xml": "text/xml"};
在文件夹下新建文件夹’myapp’并将工程页面放进去
打开cmd执行,服务端已建立
修改本地HOST
C:\Windows\System32\drivers\etc 下host文件
添加字段
127.0.0.1 localhost
至此,工作已完成可以访问本地站点了
http://localhost:3000/index.html
0 0
- 简单Node.js搭建本地站点
- 如何搭建一个简单的本地node.js服务器
- 基于node.js的简单本地服务器搭建
- 使用node.js搭建一个简单的本地服务器
- node.js 搭建本地服务
- node.js 搭建简单服务
- 【node.js】使用node.js搭建一个本地web服务器
- 使用node.js搭建本地服务器
- node.js站点备份
- 简单搭建nodeJS服务,访问本地站点文件
- 简单搭建nodeJS服务,访问本地站点文件
- 搭建一个简单的node.js服务器
- 搭建简单版的node.js
- Node.js搭建简单的环境
- 手机成功访问node.js搭建的本地服务器
- 使用node.js快速搭建简易本地HTTP服务
- Node搭建本地服务器
- 搭建本地node服务器
- java多线程:15、阻塞队列【BlockingQueue】
- decimalformat
- rapidxml 文件读写、增加、删除、编辑节点
- SPOJ 2829 TLE
- intellij idea: Error configuring listener org.springframework.web.context.ContextLoaderListener
- 简单Node.js搭建本地站点
- Miracast与Wi-Fi Direct技术简介
- Spring注解 @Component、@Controller、@Service、@Repository
- Java开发环境的配置(Android/Java开发的第一步)
- iOS常用宏定义
- cocos-2d-x学习笔记
- Spring(十一):配置DataSource
- AR多标签识别程序1
- android权限问题