我想运行一个非常简单的HTTP服务器.每个GET请求都example.com应该index.html提供给它,但作为常规HTML页面(即,与您阅读普通网页时相同的体验).
使用下面的代码,我可以阅读的内容index.html.我如何index.html作为常规网页?
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(index);
}).listen(9615);
Run Code Online (Sandbox Code Playgroud)
下面的一个建议很复杂,需要我为get我想要使用的每个资源(CSS,JavaScript,图像)文件写一行.
如何使用一些图像,CSS和JavaScript提供单个HTML页面?
我几天前刚开始尝试node.js.我已经意识到只要我的程序中有未处理的异常,Node就会终止.这与我所暴露的普通服务器容器不同,只有当未处理的异常发生且容器仍然能够接收请求时,工作线程才会死亡.这引出了一些问题:
process.on('uncaughtException')防范它的唯一有效方法吗?process.on('uncaughtException')在异步进程执行期间是否会捕获未处理的异常?我将非常感谢任何指针/文章,它将向我展示在node.js中处理未捕获的异常的常见最佳实践
Python的http.server(或Python的SimpleHTTPServer)是从命令行提供当前目录内容的好方法:
python -m http.server
Run Code Online (Sandbox Code Playgroud)
但是,就网络服务器而言,它非常低调......
它表现得好像是单线程的,并且在使用RequireJS加载JavaScript AMD模块时偶尔会导致超时错误.加载一个没有图像的简单页面可能需要五到十秒钟.
什么是更快的替代方案,同样方便?
有人知道编写jQuery扩展来处理查询字符串参数的好方法吗?我基本上想扩展jQuery魔术($)功能,所以我可以这样做:
$('?search').val();
Run Code Online (Sandbox Code Playgroud)
这将使我在下面的网址值"测试": http://www.example.com/index.php?search=test.
我已经看到很多函数可以在jQuery和Javascript中执行此操作,但实际上我希望扩展jQuery以完全按照上面所示的方式工作.我不是在寻找一个jQuery插件,我正在寻找jQuery方法的扩展.
我试图在nodejs中创建一个静态文件服务器,作为理解节点而不是完美服务器的练习.我非常了解Connect和node-static等项目,并且完全打算将这些库用于更多生产就绪的代码,但我也想了解我正在使用的基础知识.考虑到这一点,我编写了一个小型server.js:
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs');
var mimeTypes = {
"html": "text/html",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",
"js": "text/javascript",
"css": "text/css"};
http.createServer(function(req, res) {
var uri = url.parse(req.url).pathname;
var filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
if(!exists) {
console.log("not exists: " + filename);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('404 Not Found\n');
res.end();
}
var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
res.writeHead(200, mimeType);
var fileStream = fs.createReadStream(filename);
fileStream.pipe(res);
}); //end path.exists
}).listen(1337);
Run Code Online (Sandbox Code Playgroud)
我的问题是双重的
这是在节点中创建和流式传输基本html等的"正确"方法,还是有更好/更优雅/更健壮的方法?
节点中的.pipe()基本上只是执行以下操作吗?
.
var …Run Code Online (Sandbox Code Playgroud) 我只是在学习Angularjs,以及如何使用templateUrl来加载模板.
我有一个简单的指令:
var mainApp = angular.module("mainApp", []);
mainApp.directive("request", function() {
return {
restrict: "E",
scope: {
data: "@"
},
templateUrl: "te.html"
}
})
Run Code Online (Sandbox Code Playgroud)
我尝试使用这样的指令:
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-2.0.0.js"></script>
<script src="js/angular.js"></script>
<link href="css/bootstrap.css" rel="stylesheet" />
<script src="js/bootstrap.js"></script>
<script src="js/main.js"></script>
<style type="text/css">
div {
background-color: cornflowerblue;
}
#appPanel {
width: 900px;
height: 1000px;
}
</style>
</head>
<body>
<div id="appPanel" ng-app="mainApp" class="container">
<div class="row-fluid" style="height: 15%">
<div class="span12">
title
</div>
</div>
<div class="row-fluid" style="height: 85%">
<div class="span3" style="height: 100%">
actions
</div> …Run Code Online (Sandbox Code Playgroud) 我正在使用这个有角度的4种子应用程序:https://github.com/2sic/app-tutorial-angular4-hello-dnn
它使用webpack并且工作正常.
它似乎只提供开发文件而不是dist /文件夹.
我想要提供dist文件夹.
不确定执行此操作的命令,或者我是否需要安装lite服务器或其他东西.
我运行此命令来创建dist文件夹(工作正常):
g build --prod --aot --output-hashing=none
Run Code Online (Sandbox Code Playgroud)
现在我想在浏览器中运行此构建.
我正在使用一种相当丑陋的方法:
var app = require('express')(),
server = require('http').createServer(app),
fs = require('fs');
server.listen(80);
path = "/Users/my/path/";
var served_files = {};
["myfile1.html","myfile2.html","myfile3.html"].forEach(function(file){
served_files["/"+file] = fs.readFileSync(path+file,"utf8");
});
app.use(function(req,res){
if (served_files[req.path])
res.send(files[req.path]);
});
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
我写了非常简单的服务器:
/* Creating server */
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
/*Start listening*/
server.listen(8000);
Run Code Online (Sandbox Code Playgroud)
我使用nodejs运行它.
现在我想写一个简单的客户端,使用ajax调用向服务器发送请求并打印响应(Hello World)
这里是clinet的javascript:
$.ajax({
type: "GET",
url: "http://127.0.0.1:8000/" ,
success: function (data) {
console.log(data.toString);
}
});
Run Code Online (Sandbox Code Playgroud)
当我打开客户端html文件时,我在控制台中收到以下错误:
XMLHttpRequest cannot load http://127.0.0.1:8000/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
我尝试添加到以下的ajax调用:
$.ajax({
type: "GET",
url: "http://127.0.0.1:8000/" ,
dataType: 'jsonp',
crossDomain: true,
success: function (data) {
console.log(data.toString);
}
});
Run Code Online (Sandbox Code Playgroud)
但后来我明白了
Resource interpreted …Run Code Online (Sandbox Code Playgroud) node.js ×5
javascript ×2
webserver ×2
ajax ×1
angular ×1
angularjs ×1
command-line ×1
express ×1
http ×1
httpserver ×1
jquery ×1
query-string ×1
server ×1
url ×1
webpack ×1