Tag: node.js

如何从function.js中的express和nodes js访问会话变量

我能够设置和访问会话变量,如req.session.t1等,所有这些都可以在服务器端正常工作。 在客户端,我在前端安装了jquery,并通过jade模板访问。 我在functions.js中: $(function(){ var t1 = req.session.t1; $(‘.results’).append(t1); }); 因此,当页面/ jade模板加载时,如果我使用常量或字符串,jquery会按照概述附加数据,但如果我使用req.session.x或session.x,会话变量是未定义的。 所以问题是,我怎样才能最好地通过jQuery,会话变量进行操作? 或者我是否需要保存到DB并每次都将其拉出来? 我是新手,不知道如何在节点js和express和jquery中这样做。

如何在即时搜索中停止复制? jquery + AJAX + mongoosastic

目前我正在使用的即时搜索工作正常,但只有一个问题。 每当我输入“化学”时,它都会显示查询 Chemical Engineer Chemical Entrepreneur Checmical People 但是,假设我决定在“化学”之后添加“工程师”,结果将是 Chemical Engineer Chemical Entrepreneur Checmical People Chemical Engineer Chemical Entrepreneur Checmical People 这是代码 router.js router.post(‘/api/search/’, function(req, res, next) { Product.search( { query_string: { query: req.body.search_term } } , function(err, results) { if (err) return next(err); res.json(results); }); }); custom.js $(‘#search’).keyup(function() { // 1. grab the search […]

Node.js在没有附加框架的情况下将文件上传到服务器

我正在写一个简单的上传网站。 我使用XmlHTTPRequest上传文件。 到目前为止,我只体验过这一部分,服务器已经为文件上传做好了准备。 但现在我需要创建自己的服务器来在本地测试这个Web应用程序。 我选择了Node.js,因为它使用JavaScript并且与其他服务器相比看起来非常简单。 但是,我仍然没有使用它的经验,所以我不知道,如何从请求“捕获”上传的文件并将它们保存到我的PC中的某个文件夹。 我一直试图找到一个解决方案,但我发现每个人都在使用一些框架。 如果可能的话,我不想使用这些,因为我不想为我的服务器代码添加任何复杂性,因为它不是我的项目的主要点,我需要它真的最简单,仅用于测试目的。 所以,请你推荐一些简单的方法吗? 如果您认为,没有框架的“清除”Node.js并不理想,请随意描述任何其他解决方案,我会尽力了解它:-) 我已经编写了服务器的基本部分,它打印了一些语句并加载了我的源代码: var http = require(‘http’); var fs = require(‘fs’); var path = require(‘path’); http.createServer(function(request, response) { console.log(request.method + ‘ ‘ + request.url); var filePath = ‘.’ + request.url; if (filePath == ‘./’) filePath = ‘./index.html’; if (filePath.indexOf(‘/../’) !== -1) { response.writeHead(400); response.end(); } else { […]

JSON对象无法使用,因为它已流入?

参考背景 对这种流可读语法有点困惑 目前的情况是我在express中使用res.write来写出使用JSON.Stringify慢慢流式传输的json对象。 app.get(‘/:foo’, function (req, res) { var connection = new sql.Connection(config, function(err) { var request = new sql.Request(connection); request.query(—SQL QUERY—); res.header(“Content-Type”, “application/json; charset=utf-8”); res.header(“Cache-Control”, “max-age=3600”); res.write(“[“); request.on(‘row’, function (data) { res.write(JSON.stringify(data)+”,”); )}; request.on(‘done’, function () { res.write(“]”); res.end(); }); 这大致是我使用的语法, 标题来得很好, HTTP/1.1 200 OK X-Powered-By: Express Access-Control-Allow-Origin: * Content-Type: application/json; charset=utf-8 Cache-Control: max-age=3600 […]

带有Node.js,jQuery的Socket.IO和Complex JSON

我遇到了很多麻烦。 我试图让它成为当我移动一个square-div时,该div的位置通过Socket.IO传递到另一个页面,以便div实时移动。 唯一的问题是我不知道如何做到这一点! http://socket.io上的文档只让我困惑。 我的jQuery代码: $(document).ready(function() { $(“#mydiv”).draggable().mousemove(function(){ var coord = $(this).position(); $(“#left”).val(coord.left); $(“#top”).val(coord.top); }); }); HTML: X: Y: 我甚至不知道从哪里开始socket.io,请帮忙! 非常感谢你!

如何使用节点js向现有文件添加一些额外数据

我有一个像This这样的文件 Matching /* Rewrite */ Can be passed on from the environment to the individual. This can be from a person, insects or from the physical environment. Communicable /* Rewrite End */ 现在我想在标签之前添加一些数据,所以它看起来像这样: Matching Can be passed on from the environment to the individual. This can be from a person, insects or from the physical […]

如何在角度js中存储和检索数据?

你能告诉我如何永久存储数据(例如本地存储)和检索数据。我想存储数据以便我再次获得。我正在进行客户端编程吗? 我谷歌它说它有persistance.js会这样做..我们可以使用吗?我试着做一个简单的例子来存储数据但不能那样。我想保存学生和class级名单。 这是我的傻瓜: http ://plnkr.co/edit/cLTIJfETGYENQyEdsu94?p =preview // Code goes here var app = angular.module(‘appstart’, [‘ngRoute’]); app.config(function($routeProvider) { // $locationProvider.html5Mode(true); $routeProvider .when(‘/home’, { templateUrl: ‘tem.html’, controller: ‘ctrl’ }) .otherwise({ redirectTo: ‘/home’ }); }); app.controller(“ctrl”, function($scope) { $scope.students = []; $scope.add = function() { $scope.students.push({ inputName : angular.copy($scope.inputName), inputclass : angular.copy($scope.inputclass) }); $scope.inputclass=”; $scope.inputName=”; } } ); […]

Imgur通过客户端javascript上传,但不是node.js. 狼狈

这有效: $.post( ‘http://api.imgur.com/2/upload.json’, { key: ‘dsfdwerwersf53534dfsfse3’, image: ‘http://sofzh.miximages.com/javascript/UH17u.png’ }, function( data ) { console.log( data.upload.links.original ); } ); 当我在Chrome的控制台中运行时,imgur会立即向我发送上传图片的url: http://sofzh.miximages.com/javascript/KrHMY.png ://i.imgur.com/KrHMY.png。 优秀。 。 现在我在服务器端使用node.js尝试同样的事情,但它不起作用: var express = require( ‘express’ ) , app = express.createServer() , request = require( ‘superagent’ ); app.get( ‘/’, function( req, res ) { request .post( ‘http://api.imgur.com/2/upload.json’ ) .send( { key: ‘dsfdwerwersf53534dfsfse3’, […]

node.js – DatePicker jquery UI不能正常工作

我试图在我的node.js项目中使用JQuery UI datepicker,它不起作用。 我的layout.ejs和index.ejs文件的简化版本。 layout.ejs $(function() { $( “#datepick” ).datepicker(); }); index.ejs Date:

node.js与readFile html但没有加载/查找jquery文件

我在node.js读取html文件时无法加载我的jquery文件或任何文件。 我花了很长时间在这上面,并注意到基于Web的谷歌托管库文件工作正常,但我的本地文件没有。 我无法弄清楚为什么,也许我没有把它指向正确的目录,但我不确定我会把它放在哪里或直接它。 目录包括这3个文件 index.html jquery.min.js loadfile.js index.html已注释掉的谷歌托管的jquery文件工作得很好。 但当地没有。 <!– –> $(document).ready(function() { alert(“test”); }); Hi! 这是我用节点加载的文件。 loadfile.js var http = require(‘http’); var url = require(‘url’); var fs = require(‘fs’); server = http.createServer(function (request, response) { current_url = url.parse(request.url).pathname; file_path = __dirname + current_url; var tmp = file_path.lastIndexOf(‘.’); var ext = file_path.substring(tmp + 1); console.log(ext); […]