node.js

来源:互联网 发布:初级php开发工程师 编辑:程序博客网 时间:2024/06/15 12:38
// 创建服务器
//路由


// 引入http模块,里面封装了又创建服务器的方法
var http = require('http');


// createServer创建服务器,把服务器返回给我们
// 参数是一个回调函数:
var server = http.createServer(function(req,res) {
    //code
    res.writeHead(200,{'Content-type':'text/plain;charset=utf-8'});


    res.write('<h1>张猛</h1>');
    // end表明结束,必须的有
    res.end();
});
// 给服务器设置路径,端口等,
// 第一个参数:端口,第二个参数:路径,第三个参数:回调函数,检测服务器是否启动成功
//如果启动失败,会把失败信息给我们err
server.listen(8888,'localhost',function(err) {
    // 根据是否返回了失败信息确定是否启动成功
    if (err) {
        console.log(err);
    } else {
        console.log('服务器启动成功');
    }
})


// 吧crateServer和liston连在一起
// http.createServer(function (req,res) {
//
// }).listen(8888);

// express模块,对http做的更深层次的封装

//npm :他是node里面管理各个模块的

var express = require('express');
var app = express();
app.get('*',function (req,res) {
    res.sendFile(__dirname+req.url);
})


app.listen(8888,'localhost',function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log('服务器启动成功');
    }
});

命令:

node —>server.js

npm  install  express 安装express  模块

cd  进入目录;

cd..   返回上级目录;

del  删除文件

dir  列出文件列表;

cls  清屏


0 0
原创粉丝点击