我想知道为什么很难在一个简单的JSON字符串中发布/:parameter以解决问题.我遵循了许多例子,但没有发现任何具体的东西.
我在前端有以下代码.
$("#btnDoTest").click(function() {
var jData = {
hello: "world"
};
var request = $.ajax({
url: "http://localhost:8081/j/",
async: false,
type: "POST",
data: JSON.stringify(jData),
contentType: "application/javascript",
dataType: "json"
});
request.success(function(result) {
console.log(result);
});
request.fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
});
Run Code Online (Sandbox Code Playgroud)
如果我在后面连接param,我在发送简单文本方面是成功的j/.但我要发送的是这样的对象,{hello:"world"}并在nodeJS中重新构建它并使用它.
- 编辑:
This is my nodejs file
/* the below function is from restifylib/response.js */
var restify = require("restify");
/* create the restify server */
var server = restify.createServer({
});
server.use(restify.bodyParser({ …Run Code Online (Sandbox Code Playgroud) 我有一个 NodeJs REST 服务,我们称之为 -后端的NodeRest和前端的 AngularJs。
NodeRest应该与移动应用程序以及 Web 应用程序一起使用,在我的情况下它是 AngularJs 应用程序。
NodeRest 的架构在使用 PassportJs 时应该解决以下问题:
服务器不应将用户重定向到 Facebook 以在何时进行授权
app.get('/auth/facebook', passport.authenticate('facebook'));
Run Code Online (Sandbox Code Playgroud)
已被调用。
如果它要重定向它,客户端将不会得到任何东西,因为回调 url 链接到NodeRest httpL//noderest/facebook/callback。相反,它应该提供重定向 uri,以便我可以将其发送回客户端(angularJs、mobile 等...)。像这样:
app.get('/auth/facebook', passport.authenticate('facebook', function(redirectUri){
//emit socket event to the client with redirect uri as a response data. }));
Run Code Online (Sandbox Code Playgroud)
我决定在授权过程中使用 socket.io 作为通信渠道。
客户:
var socket = io.connect(baseUrl);
socket.on('auth:facebook:callback:getCalled', function (data) {
// callback get called on server side.
// user has been authenicated.
// so now, user can talk with our …Run Code Online (Sandbox Code Playgroud) 我的REST api heroku应用程序(使用restify)崩溃了,我不知道为什么。在heroku日志中只有一行:
at=error code=H10 desc="App crashed" method=GET path=/some/api/path host=some.host.com fwd="83.28.44.34" dyno= connect= service= status=503 bytes=
Run Code Online (Sandbox Code Playgroud)
我尝试添加以下内容:
process.on('uncaughtException',function(err){
console.log('#########');
console.log(err);
throw err;
});
Run Code Online (Sandbox Code Playgroud)
但是它不会向日志写入任何内容。
问题是我不知道应用程序是否在http请求期间崩溃,因为即使请求完成后也可能会触发某些功能...
如何跟踪导致我的应用崩溃的原因?
我一直在使用node-restify测试它的lil应用程序中实现CORS,事实证明,在浏览器中,行为是预期的,这意味着,在CORS禁用的不同来源,它不会工作,如果CORS启用,它的工作原理.
然而,棘手的部分是使用CURL,它始终有效!我一直在关注这个问题: 如何使用cURL调试CORS请求?
我这样做:
curl -H 'Origin: http://example.com' http://cors.somewhere.com
Run Code Online (Sandbox Code Playgroud)
并使用node-restify示例进行调试
var restify = require('restify');
var srv = restify.createServer();
//srv.use(restify.CORS()); // I enable and disable by uncomment line
function foo(req, res, next) {
res.send("bananas");
next();
}
srv.put('/foo', foo);
srv.get('/foo', foo);
srv.del('/foo', foo);
srv.post('/foo', foo);
srv.listen(process.env.PORT || 8080);
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
谢谢!
我无法使用restify对我的node.js API进行url编码的帖子.我有以下设置我的restify应用程序:
app.use(restify.acceptParser(app.acceptable));
app.use(restify.queryParser());
app.use(restify.urlEncodedBodyParser());
Run Code Online (Sandbox Code Playgroud)
但是,当我使用curl请求我的应用程序时,请求:
curl -X POST -H "Content-type: application/x-www-form-urlencoded" -d quantity=50 http://app:5000/feeds
Run Code Online (Sandbox Code Playgroud)
我在视图中得到以下输入体:
console.log(req.body) // "quantity=50"
Run Code Online (Sandbox Code Playgroud)
提前致谢,
马蒂亚斯
我对node.js服务很新,我遇到了multipart/form-data内容类型的问题.我需要一种方法来禁用特定请求的主体解析器功能.我正在使用restify 2.6.1.以下是配置的一些片段.
我的设置是:
App.js :
server.use(restify.authorizationParser());
server.use(restify.dateParser());
server.use(restify.queryParser());
server.use(restify.jsonp());
server.use(restify.bodyParser());
server.use(restifyValidator);
server.use(restify.gzipResponse());
server.use(passport.initialize());
server.use(restify.conditionalRequest());
Route.js :
app.post({path: '/test/upload/:upload_image_name', version: ver}, uploadCtr.uploadImage);
app.post( {path: '/test/upload/:upload_image_name', version:ver }, passport.authenticate('bearer',{ session: false}),uploadCtr.uploadImage);
Run Code Online (Sandbox Code Playgroud)
没有restify.bodyParser()上传图像正在工作(但依赖于json解析器的所有内容都失败了)
提前致谢.
我正在遵循官方的解决指南来使用socketio和restify.
api.js
var mongoose = require('mongoose');
var restify = require('restify');
var fs = require('fs');
var server = restify.createServer({
name: 'myapp',
version: '1.0.0'
});
var io = require('socket.io')(server);
server.get('/', function indexHTML(req, res, next) {
fs.readFile(__dirname + '/sockettest.html', function (err, data) {
if (err) {
next(err);
return;
}
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(data);
next();
});
});
io.on('connection', function(socket){
console.log('a user connected');
});
Run Code Online (Sandbox Code Playgroud)
sockettest.html
<html>
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
<script>
var socket = io();
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
当我浏览到localhost:3000我收到此错误时:
myapp listening at http://[::]:3000
_http_outgoing.js:350 …Run Code Online (Sandbox Code Playgroud) 我还是节点 js 的新手。是否有任何解决方法或方法可以使用 node js 识别来自客户端的请求是来自移动设备还是非移动设备?因为我现在正在做的是我想根据设备类型(移动/桌面)限制对某些 API 的访问。我在服务器端使用restify。谢谢。
如何使用 node-passport-restify 允许每个用户进行一个活动会话?IE; 不允许用户同时使用其他选项卡或浏览器在多个会话中处于活动状态。
这是运行应用程序的代码。
const
restify = require('restify'),
restifyPlugins = require('restify').plugins,
passport = require('passport'),
BearerStrategy = require('passport-azure-ad').BearerStrategy,
config = require('./config'),
authenticatedUserTokens = [],
serverPort = process.env.PORT || config.serverPort;
const authenticationStrategy = new BearerStrategy(config.credentials, (token, done) => {
let currentUser = null;
let userToken = authenticatedUserTokens.find((user) => {
currentUser = user;
user.sub === token.sub;
});
if (!userToken) {
authenticatedUserTokens.push(token);
}
return done(null, currentUser, token);
});
passport.use(authenticationStrategy);
const server = restify.createServer({
name: 'My App'
});
server.use(restifyPlugins.acceptParser(server.acceptable));
server.use(restifyPlugins.queryParser());
server.use(restifyPlugins.fullResponse());
server.use(restifyPlugins.bodyParser({
maxBodySize: 0, …Run Code Online (Sandbox Code Playgroud) 我将Typeorm与class-validator结合使用,所以我定义了一个像这样的实体:
import {
Entity,
PrimaryGeneratedColumn,
Column,
BaseEntity,
BeforeInsert,
BeforeUpdate,
getRepository
} from "typeorm";
import {
validateOrReject,
IsDefined,
} from "class-validator";
import errors from 'restify-errors';
@Entity()
export class License extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
public id!: string;
@Column({ nullable: false })
@IsDefined({ message: 'name field was not provided' })
public name!: string;
@Column({ nullable: false })
@IsDefined({ message: 'description field was not provided' })
public description!: string;
@BeforeInsert()
@BeforeUpdate()
async validate() {
await validateOrReject(this, { skipUndefinedProperties: true });
// …Run Code Online (Sandbox Code Playgroud) restify ×10
node.js ×9
passport.js ×2
rest ×2
adal ×1
ajax ×1
angularjs ×1
cors ×1
curl ×1
heroku ×1
httprequest ×1
javascript ×1
json ×1
socket.io ×1
sockets ×1
typeorm ×1
typescript ×1
validation ×1