赞
踩
- var http = require('http');
- http.createServer(function(req, res){
- var html = '<html>'
- +'<head>'
- +'<title>nodejs</title>'
- +'</head>'
- +'<body>'
- + 'hello world! 1234'
- +'</body>'
- +'</html>';
- res.writeHead(200,{'Content-Type' : 'text/html'});
- res.write(html);
- res.end();
- }).listen(8888);
在浏览器地址栏输入127.0.0.1:8888查看结果,打开控制台,可以发现网页的类容已经全部包含在浏览器中。
- var http = require('http');
- http.createServer(function(req, res){
- var html = '<html>'
- +'<head>'
- +'<title>nodejs</title>'
- +'<link rel="stylesheet" type="text/css" href="./bootstrap/css/bootstrap.min.css" />'
- +'<script type="text/javascript" src="./bootstrap/js/bootstrap.min.js"></script>'
- +'</head>'
- +'<body>'
- + 'hello world!hello world! 1234'
- +'</body>'
- +'</html>';
- res.writeHead(200,{'Content-Type' : 'text/html'});
- res.write(html);
- res.end();
- }).listen(8888);

会发现css文件和javascript文件都没有被正确下载。这是因为这段代码中规定的'Content-Type'都是'text/html'类型,而且所有的response内容都相同,当然就看不到想要的效果。
- var http = require('http');
- http.createServer(function(req, res){
- var html = '<html>'
- +'<head>'
- +'<title>nodejs</title>'
- +'<style type="text/css">'
- +'body{color:red;}'
- +'</style>'
- +'</head>'
- +'<body>'
- + 'hello world! 1234'
- +'</body>'
- +'</html>';
- res.writeHead(200,{'Content-Type' : 'text/html'});
- res.write(html);
- res.end();
- }).listen(8888);

可以看到浏览器中的文字显示为红色。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。