如何在nodejs中发送文件,我正在创建一个页面应用程序,所以我只想abcd.html页面首次按要求交付,这是我的代码
app.js
var express = require('express'),
app = express(),
http = require('http'),
path = require('path'),
fs = require('fs'),
mysql = require('mysql'),
server = http.createServer(app),
passport = require('passport'),
flash = require('connect-flash'),
useragent = require('express-useragent'),
io = require('socket.io').listen(server);
// configuration ===============================================================
// connect to our database
require('./config/passport')(passport); // pass passport for configuration
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
app.configure(function() {
app.set('views', path.join(__dirname, 'app/views'));
app.set('view engine', 'ejs'); // set up ejs for templating
// required for passport
app.use(express.session({secret: 'vidyapathaisalwaysrunning',key: 'myuser.sid',cookie: { secure: false,maxAge:null}} )); // session secret1
//app.use(express.session({secret: 'vidyapathaisalwaysrunning', key: 'myownsid', cookie: { secure: false, maxAge: 3600000 }} )); // session with expitation
//app.use(express.session({ key: 'express.sid', secret: 'vidyapathaisalwaysrunning' } )); // 2 cookieId ie connect.sid & express.sid(user defined) session secret1
//app.use(express.session({ store: sessionStore, secret: 'vidyapathaisalwaysrunning',cookie: {httpOnly: false},key: 'cookie.sid' } )); // session secret
// set up our express application
app.use(express.logger('dev')); // log every request to the console
app.use(express.cookieParser()); // read cookies (needed for auth)
app.use(express.bodyParser()); // get information from html forms
app.use(useragent.express());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
app.use(express.static(path.join(__dirname, 'public')));
});
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
//routes ======================================================================
require('./app/controller.js')(app, passport,io); // load our routes and pass in our app and fully configured passport
//require('./app/socket')(app,io);
// launch ======================================================================
server.listen(8080);
对页面的响应
case '/':
/*response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');*/
//response.json({ message: 'hello' });
response.sendfile("../public/abcd.html");
break;
发送后,它总是给出无法设置的标题,下面是控制台输出
错误:SendStream禁止。SendStream出错(/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/NODE\u modules/send/lib/send.js:145:16)。ServerResponse上的管道(/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/NODE\u modules/send/lib/send.js:307:39)。res.sendfile(/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/lib/response.js:339:8)位于/home/pitu/CODING/NODE-PROJECTS/chichat/app/controller。js:18:21在pass(/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/lib/router/index.js:161:37)的param(/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/lib/router/index.js:135:11)在路由器的pass(/home/pitu/CODING/NODE\u modules/chichat/router/index.js:142:5)_在对象上调度(/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/lib/router/index.js:170:5)。路由器(/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/lib/router/index.js:33:10)在下一个(/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/NODE\u modules/connect/lib/proto.js:190:15)\u http\u传出。js:331抛出新错误('发送邮件后无法设置邮件头');^错误:发送邮件后无法设置邮件头。服务器响应。传出消息。ServerResponse上的setHeader(\u http\u outgoing.js:331:11)。res.setHeader(/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/NODE\u modules/connect/lib/patch.js:59:22)位于/home/pitu/CODING/NODE-PROJECTS/chichat/NODE\u modules/express/NODE\u modules/connect/lib/middleware/errorHandler。js:63:17,fs。js:292:14 at对象。完成(fs.js:93:15)
var event= require('./models/event');
var url = require('url');
var fs = require("fs");
//var app1 = require('../../app').app;
module.exports = function(app, passport,io)
{
//app.get('/createevent',event.createevent);
app.get('*', function(request, response)
{
var path = url.parse(request.url).pathname;
//use request object to get user data
//response.render('index');
switch(path){
case '/':
/*response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');*/
//response.json({ message: 'hello' });
//response.sendfile('../../public/abcd.htm', {root: __dirname })
response.sendfile('./public/abcd.htm');
break;
case '/creteevent':
console.log(" inside createevent ");
response.render('index123',{ title: 'The index page!' });
break;
case '/eventmember':
//response.render('index');
//this method is good becz below code , calling same as 4 both socket & http req
event.eventmember(function(err,returnedvalue)
{
if(returnedvalue){
console.log(" member result "+returnedvalue);
response.json(returnedvalue);
}else{
console.log(" member err "+ err);
}
});
break;
case '/eventtags':
var str = '{"eventid" :"2","associatedtags":["2","3"]}';
//response.render('index');
//this method is good becz below code , calling same as 4 both socket & http req
event.eventtags(str,function(err,returnedvalue)
{
if(returnedvalue){
console.log(" post result "+returnedvalue);
}else{
console.log(" posting err "+ err);
}
});
break;
case '/post':
var str = '{"eventid" :"2","selfid" :"2","title" :"feeedback to event","content" :"bla bla bla","lattitude" :"","longitude" :"","createddatetime" :"","posttype" :"idea","isapporved" :"no","isanonymous" :"no"}';
//response.render('index');
//this method is good becz below code , calling same as 4 both socket & http req
event.post(str,function(err,returnedvalue)
{
if(returnedvalue){
console.log(" post result "+returnedvalue);
}else{
console.log(" posting err "+ err);
}
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
break;
}
//just remove this line,& it worked, becz 2 response for a single request
***response.end();***
});
};
问题内容: 将标头发送到客户端后,无法设置标头。这是请求验证后的后端错误,看起来像标头问题。我为我的项目脏代码感到抱歉,我还需要做其他事情,因此在注释中有一些代码。这是我的代码 您可以在我的代码中发现其他错误,因为我尝试了许多不同的方法。这是passport.js配置文件 这是身份验证的ajax请求 非常感谢你。 问题答案: 这是由于您的代码试图从authenticate函数发送多个响应。 删除其
问题内容: 我不太确定为什么会收到此错误。这是一个基于express.js的简单API,能够添加和删除帖子。当我触发删除路由器时发生错误。我读到错误通常在有两个回调的情况下发生,但是,我似乎找不到任何双重回调。 这是我的posts.js路由器: 问题答案: 您需要添加“返回”,这样您就不会再次回答。
将标头发送到客户端后,无法对其进行设置。这是我的身份验证post请求后的后端错误。看起来标题有问题。我很抱歉我的项目肮脏的代码,我需要做一些别的事情,所以有一些代码在评论中。这是我的代码 你可以在我的代码中找到其他错误,因为我尝试了许多不同的方法。以下是passport.js配置文件 这是用于身份验证的 ajax 请求 非常感谢。
我在NodeJS中使用Passport.js、Express和Mongoose时遇到了这个奇怪的问题。基本上,即使我只发送了一个头,我还是得到一个错误,说“不能在头被发送到客户端后设置它们”。 我读过其他帖子并尝试过,但没有一个有效。 应用程序。get-res.send与return res.send之间有什么区别吗 错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后,无法设
问题内容: 我对Node.js相当陌生,遇到了一些问题。 我正在使用Node.js 4.10和Express 2.4.3。 当我尝试访问http://127.0.0.1:8888/auth/facebook时,我将重定向到http://127.0.0.1:8888/auth/facebook_callback。 然后,我收到以下错误: 以下是我的代码: 我可以知道我的代码有什么问题吗? 问题答案:
我正在关注一个关于udemy上的节点.js的课程,这有点过时了,遇到了这些错误,我无法找到解决方案。 我尝试了什么: 使用next(); 在所有if语句中添加返回res 有人能帮我修一下这些吗?我将不胜感激! 提前感谢! 用户名存在错误:错误 [ERR_HTTP_HEADERS_SENT]:在服务器响应.set 上将标头发送到客户端后无法设置标头 (_http_outgoing.js:526:11