node.js

作者: 灬感同身受灬 | 来源:发表于2017-02-15 11:36 被阅读0次

nodejs 安装

sudo apt-get install nodejs
sudo apt-get install npm```



#nodejs server代码配置环境

var http = require('http');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
var cache = {};

function send404(response) {
response.writeHead(404, {"Content-Type": text / plain});
response.write('Error 404 ');
response.end();
}
function sendFile(response, filePath, fileContents) {
response.writeHead(200, {"Content-type": mime.lookup(path.basename(filePath))});
response.end(fileContents);
}
function serveStatic(response, cache, absPath) {
if (cache[absPath]) {
console.log(cache[absPath])
sendFile(response, absPath, cache[absPath])
} else {
fs.exists(absPath, function (exists) {
if (exists) {
fs.readFile(absPath, function (err, data) {
if (err) {
send404(response)
} else {
console.log(data);
cache[absPath]=data
sendFile(response,absPath,data)
}
})
}else {
console.log('5');
send404(response)
}
})
}
}
var server = http.createServer(function(request,response){
var filePath = false;
if (request.url=='/'){
filePath='index.html';
}else{
filePath=request.url;
}
console.log(filePath);
var absPath='./'+filePath;
console.log(absPath);
serveStatic(response,cache,absPath);
})
server.listen(3000,function(){
console.log('ok');
})```

所需要的模块

Paste_Image.png
  1. 创建服务器的代码
Paste_Image.png
  1. 使用了监听事件回调函数


    Paste_Image.png
  2. 吧http服务变成一个模块

Paste_Image.png

引用创建的模块

Paste_Image.png

项目

  1. models 项目模型
  2. views 视图
  3. roune路由 controller控制
  4. uploads 文件上传文件夹
  5. public 静态文件
  6. app.js 主文件
  7. node_modules npm下载的文件夹

相关文章

  • nodejs安装

    Node.js安装 目录 Node.js简单介绍 windows安装Node.js Linux安装Node.js ...

  • node.js基础

    什么是node.js Node.js特点 node.js优点和缺点

  • Nodejs.2

    参考内容:Node.js EventEmitter 四、Node.js EventEmitter Node.js所...

  • nodejs第一步

    Node.js 是什么?Node.js与JavaScript的区别是什么? Node.js的优点?Node.js的...

  • node 学习笔记.md

    Node.js第一天 1. 初识Node.js 1.1 Node.js是什么 Node.js® is a Java...

  • Node.js学习

    主线:Node.js是什么 --> Node.js的组成 --> Node.js的特点 --> Helloworl...

  • 使用AngularJS搭建前台框架

    Node.js部署: 下载安装包:从Node.js官网下载Node.js安装包。 安装Node.js:打开node...

  • Node.js模块

    Node.js 模块和 Node.js 包介绍。 一、Node.js模块 每一个Node.js都是一个Node.j...

  • 小鹅通视频下载mac 小鹅通课程下载教程

    前两天,Node.js官方发布了Node.js 15的正式版本,Node.js 15 将替代 Node.js 14...

  • node学习笔记

    node.js 介绍 node.js初识 node.js 平台是基于 Chrome V8 JavaScript 引...

网友评论

      本文标题:node.js

      本文链接:https://www.haomeiwen.com/subject/swlputtx.html