我在使用 expressjs 时遇到错误。它说“无法获取/register.html”。我不知道如何解决这个问题。我想向/register 显示 register.html,但我总是出错。顺便说一句,这是我的代码人员。谢谢大家,我希望你能帮助我,我已经用谷歌搜索了这个错误 1 小时,但我仍然无法让它工作。
app.js
var express = require("express");
var app = express();
var redis = require("redis");
var client = redis.createClient();
var reply;
app.use(express.bodyParser());
client.on("error", function (err) {
console.log("error event - " + client.host + ":" + client.port + " - " + err);
});
app.get("/", function (req, res) {
res.sendfile(__dirname + "/login.html");
});
app.get("/", function (req, res) {
res.sendfile(__dirname + "/register.html");
});
app.post("/login", function (req, res) {
res.sendfile(__dirname + "/login.html");
console.log("--------------------");
console.log(req.body.user.username);
console.log(req.body.user.password);
console.log("--------EOF---------");
client.hmget( "credentials", req.body.user.username, function (err,pass) {
console.log("req.body.username: " + req.body.user.username);
console.log("req.body.password: " + req.body.user.password);
console.log("pass: " + pass );
console.log("err: " + err );
if ( (!err) && pass && pass == req.body.user.password )
res.write( "Successfully logged in!" );
else if ( pass == false)
res.write("Account Does not Exist")
else
res.write( "Password is incorrect");
res.end();
});
});
app.post("/register", function (req, res) {
res.redirect("/register.html");
console.log("--------------------");
console.log(req.body.user.fname);
console.log(req.body.user.lname);
console.log(req.body.user.username);
console.log(req.body.user.pass);
console.log(req.body.user.email);
console.log(req.body.user.mobile);
console.log(req.body.user.mydate);
console.log("--------EOF---------");
client.hmset("credentials",req.body.user.uname, "first_name",req.body.user.fname,
"last_name",req.body.user.lname, "username", req.body.user.uname,
"password", req.body.user.pass, "email", req.body.user.email,
"mobile", req.body.user.mobile, redis.print);
res.write("data inserted");
res.end();
});
app.listen(1337);
这是我的 login.html
<html>
<head></head>
<body>
<p>
<form method="post" action="/login">
Username: <input type="text" name="user[username]"> <br>
Password: <input type="text" name="user[password]"> <br>
<input type="submit" value="Submit"> <br>
<a href="/register.html">Register!</a>
</form>
</p>
</body>
</html>
这是我的 register.html
<html>
<head></head>
<body>
<p>
<form method="post" action="/register">
First Name: <input type="text" name="user[fname]"> <br>
Last Name: <input type="text" name="user[lname]"> <br>
Username: <input type="text" name="user[username]" > <br>
Password: <input type="text" name="user[password]"> <br>
Email: <input type="text" name="user[email]"> <br>
Mobile Number: <input type="text" name="user[mobile]"> <br>
<input type="submit" value="Submit">
</p>
</form>
</body>
</html>
最佳答案
/register 的路由应该是这样的:
app.get("/register", function (req, res) {
res.sendfile(__dirname + "/register.html");
});
目前您已将登录和注册 View 都放在“/”上,但只有第一个可以工作。
关于html - ExpressJs 无法获取/注册错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18487356/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test