在Node.js中读取文件

我对Node.js中的文件感到困惑。 fs.open('./start.html', 'r', function(err, fileToRead){ if (!err){ fs.readFile(fileToRead, {encoding: 'utf-8'}, function(err,data){ if (!err){ console.log('received data: ' + data); response.writeHead(200, {'Content-Type': 'text/html'}); response.write(data); response.end(); }else{ console.log(err); } }); }else{ console.log(err); } }); 文件start.html与文件在同一个目录下,试图打开并阅读它。 但是,在控制台,我得到: {[Error:ENOENT,open'./start.html'] errno:34,code:'ENOENT',path:'./start.html'} 有任何想法吗?

Node.js和Microsoft SQL Server

有什么办法可以让我的Node.js应用程序与Microsoft SQL进行通信? 我还没有看到任何MS SQL驱动程序在野外? 我把一个非常简单的应用程序放在一起,需要能够与现有的MS SQL数据库进行通信(否则我会用mongoDB或Redis)

在执行查询时防止将Sequelize输出到控制台?

我有一个function来检索用户的configuration文件。 app.get('/api/user/profile', function (request, response) { // Create the default error container var error = new Error(); var User = db.User; User.find({ where: { emailAddress: request.user.username} }).then(function(user) { if(!user) { error.status = 500; error.message = "ERROR_INVALID_USER"; error.code = 301; return next(error); } // Build the profile from the user object profile = { "firstName": user.firstName, […]

Access-Control-Allow-Origin不允许源http:// localhost:3000

XMLHttpRequest cannot load http://localhost:8080/api/test. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin. 我阅读了关于跨域的Ajax请求,并了解了底层的安全问题。 就我而言,2台服务器在本地运行,并且喜欢在testing期间启用跨域请求。 localhost:8080 – Google Appengine dev server localhost:3000 – Node.js server 我正在向localhost:8080 – GAE server发出一个ajax请求,而我的页面是从节点服务器加载的。 什么是最简单,最安全的(不想用disable-web-security选项启动Chrome)。 如果我必须更改'Content-Type' ,我应该在节点服务器上执行吗? 怎么样?

翡翠/帕格评论

我尝试在Jade / pug中做出评论,但评论在HTML中作为文本呈现。 这是我的代码: doctype html html(lang='en') body / This should be a comment 我在做什么愚蠢的事情?

用node-inspectordebuggingnode.js

我正在尝试使用节点检查器来debugging我的nodejs应用程序。 但谷歌浏览器不显示代码。 我正在使用以下, Node.js:v0.10.26 Express:4.0.0 节点检查器:v0.7.3 Google Chrome版本:34.0.1847.131 这是我正在做的启动debugging器.. $ node-inspector Node Inspector v0.7.3 Visit http://127.0.0.1:8080/debug?port=5858 to start debugging. 在另一个控制台, $ node –debug app.js debugger listening on port 5858 $ 然后开始谷歌浏览器,并去 http://127.0.0.1:8080/debug?port=5858 它打开节点检查器,但没有任何代码..所有的窗口是空的。 注意到我没有得到“Express服务器在端口3000上侦听” 尝试所有的节点检查员无法连接到节点,但没有运气 无法解决我错过的东西。 这将是伟大的你有任何build议..所以我可以在Google Chrome中debugging我的Node.js应用程序。

如何在不定义模式的情况下使用Mongoose?

在之前版本的Mongoose(对于node.js)中,有一个选项可以在不定义模式的情况下使用它 var collection = mongoose.noSchema(db, "User"); 但在目前的版本中,“noSchema”function已被删除。 我的模式可能经常改变,真的不适合定义的模式,所以有没有一种新的方法在mongoose中使用无模式模型?

nodejs中的单例模式 – 是否需要?

我最近遇到了关于如何在Node.js中编写单例的文章。 我知道require的文档说: 模块在第一次加载后被caching。 多次调用require('foo')可能不会导致模块代码被多次执行。 所以看起来,每个需要的模块都可以很容易地使用一个单例,而不需要单独的样板代码。 题: 上面的文章是否提供了创build单例的解决scheme?

在Express 4和express-generator的/ bin / www中使用socket.io

所以这里是交易:我试图在一个快速项目中使用socket.io。 在Express Js 4发布后,我更新了我的快速生成器,现在应用程序的初始function进入./bin/www文件,包括那些variables(www文件内容: http : //jsfiddle.net/avMa5/ ) var server = app.listen(app.get('port'), function() {..} (通过npm install -g express-generator ,然后express myApp 就这么说吧,让我们回忆一下socket.io文档是如何要求我们解雇的: var app = require('express').createServer(); var io = require('socket.io')(app); 好吧,但我不能在app.js里面做,就像推荐一样。 这应该在./bin/www中完成才能工作。 在./bin/www这是我能做些什么来得到它的工作: var io = require('socket.io')(server) 确定这个工程,但我不能在任何地方使用io var,我真的不想把我的socket.io函数在www文件。 我想这只是基本的语法,但我不能得到这个工作,甚至没有使用module.exports = server或server.exports = server或module.exports.io = app(io)上的WWW文件 所以问题是:我如何使用socket.io这个/ bin / www文件作为我的应用程序的起点?

Node.js请求对象文档?

这是一个非常简单的问题,但我没有在Google上find任何东西。 我正在寻找在Node.js的创build服务器function请求参数的文档,但我一直无法find任何东西。 http.createServer(function(request, response){ console.log(JSON.stringify(request)); }); 用JSON.stringify()debugging给我一个错误,对象是循环的,程序停止。 我见过不同的东西,如request.url或request.body ,但有没有一个页面logging所有的请求函数和参数? 似乎应该很容易find,我似乎无法find它。 我再次尝试,结果只是做console.log(request)写出console.log(request)所有数据。 这里只是作为参考: ondata: [Function], _httpMessage: { domain: null, _events: [Object], _maxListeners: 10, output: [], outputEncodings: [], writable: true, _last: false, chunkedEncoding: false, shouldKeepAlive: true, useChunkedEncodingByDefault: true, sendDate: true, _hasBody: true, _trailer: '', finished: false, _hangupClose: false, socket: [Circular], connection: [Circular] } }, connection: { _connecting: […]