Tag: 表示

如何从jQuery渲染ejs?

我想从API获取数据后呈现文件。 码 $(document).ready(function() { $(document).delegate( "#editUser", "click", function() { var userId = $(this).data('id'); $.post("/userProfile",{"userId":userId},function(data, status){ console.log(data); //gets data here //how to render ejs file here with the above data? }); }); ejs文件: (sample.ejs) <% include header.html %> <strong>Sample ejs</strong> <ul> <li><%= data.name %> says: <%= data.message %></li> </ul> <% include footer.html %> 我怎么解决这个问题?

节点快递app.all不能路由到所需的模块

我有2个文件:extfile.js routes = require('express').Router(); function CB (req,res) { res.send('Hello from file'); }; routes.all('/', CB); module.exports = routes; 和app.js: App = require('express')(); CB2 = require('./extfile'); function CB (req,res) { res.send('Hello world'); }; App.all('/', CB); // Fine Alt 1 App.all('/', CB2); // Fine! Alt 2 App.all('/test', CB); // Fine App.all('/t2', CB2); // Gives: "Cannot GET /t2" ??? […]

Express.js – 针对静态文件的GET在Mozilla中失败

我是一个新手来expression,试图通过明确的静态机制服务于一个html文件。 以下代码正确显示了Windows 10上的Microsoft Edge中的html文件,但使用Mozilla时出现错误Cannot GET /designer.html/ ,并且URL会自动从http://localhost:3000/designer.html http://localhost:3000/designer.html/更改为http://localhost:3000/designer.html/ http://localhost:3000/designer.html 我只想知道我是在做一些愚蠢的事情,还是真的是一个错误。 'use strict'; const express = require('express'); const app = express(); const path = require('path'); let dir = path.join(__dirname, 'views'); app.use(express.static(dir)); app.listen(3000, () => { console.log('Designer listening on port 3000.'); });

expressjs中间件的“res.sendFile”和angular度客户端的“ng-src”的组合不起作用

我正尝试用头像图片创build用户configuration文件; 我用multer成功上传了头像图片; 现在我正试图提供一个选项来编辑configuration文件; 作为编辑的一部分,我必须在客户端显示头像图片; 我已经在中间件中写了一个服务来使用res.sendFile返回头像图像; 中间件控制器: exports.readAvatar = function(req, res) { if (req.user) { res.sendFile(path.resolve(__dirname + "/../../uploads/" + req.user.avatar)); } } 我甚至从浏览器testing这个服务,并成功的工作,但是当我试图显示这个化身服务使用ng-src与数据url像下面的响应,它没有工作。 客户控制器: Users.avatar($routeParams.userId).then(function(data) { $scope.avatar = "data:image/jpeg;base64,"+data; }, function(errorResponse) { $scope.error = errorResponse.message }); HTML: <img data-ng-src="{{avatar}}" > 当我GOOGLE时,我发现res.sendFile实际上是发送一个字节stream; 所以我试图把它转换成一个string,然后编码转换的string,并在ng-src中使用这个编码的string,但无济于事。 任何帮助将不胜感激/

Graphql:必须提供查询string

我有一个简单的快递graphql服务器: const schema = require('./schema'); const express = require('express'); const graphqlHTTP = require('express-graphql'); const cors = require('cors') const bodyParser = require('body-parser'); const app = express(); app.use(cors()); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use('/graphql', graphqlHTTP(req => { return ({ schema, pretty: true, }) })); const server = app.listen(9000, err => { if (err) { return err; } […]

为什么使用“”快速附加对象数组名称

我正在使用Node.js / Express.js后端。 在前端,我以这种方式将一个对象传递给我的后端API。 var accountData = {"my_tags" : ["tag1", "tag2", "tag3"]} $.post("/api/submission/1", accountData, function(sucess){ console.log("success"); }); 在后端app.js是这样设置的 var app = express(); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', index); app.use('/api', api); 然后在我的api router.post("/api/submission/:pageNumber", function (req, res) { console.log(JSON.stringify(req.body)); 打印{“my_tags []”:[“tag1”,“tag2”,“tag3”]} 为什么Node或Express将“[]”附加到“my_tags”对象数组的名称上?

使用Express进行Vue.js路由

我在这里创build了一个组合https://temp-portfolio.herokuapp.com 。 我的问题是,当用户刷新除index.html以外的页面或直接到另一个链接,如https://temp-portfolio.herokuapp.com/projects路由不呈现。 我已经使用Node.js和Express在Heroku按照本指南设置了一切https://medium.com/@sagarjauhari/quick-n-clean-way-to-deploy-vue-webpack-apps-on-heroku- b522d3904bc8 。 我试图重写服务器到这个https://stackoverflow.com/a/44226999/4178637,但应用程序崩溃。 server.js var express = require('express'); var path = require('path'); var serveStatic = require('serve-static'); app = express(); app.use(serveStatic(__dirname)); var port = process.env.PORT || 5000; app.listen(port); console.log('server started '+ port); index.js import Vue from 'vue' import Router from 'vue-router' import Nav from '@/components/Nav' import Home from '@/components/Home' import Projects from […]

在中间件中发出一个Post请求

我是Express JS和Node JS的新手。 我打算在Express Middleware中手动实现Auth-Server,我使用https://github.com/ranm8/requestify发出请求 const requestify = require('requestify'); app.use(function (req, res, next) { var config = require('./config-' + process.env.REACT_APP_ENV) var redirect = config.ADMIN_UI_URL // Perform login to Auth-Server if (req.originalUrl.startsWith('/?code')) { let auth_code = req.query.code let params = { 'grant_type': 'authorization_code', 'code': auth_code, 'client_id': config.OAUTH_CLIENT_ID, 'redirect_uri': redirect } requestify.post(config.OAUTH_ID_URL+'/oauth2/token', params).then(function(response) { console.log(response.getBody()); // There […]

如何确定用户在使用护照时注销?

今天开始学习节点,我几乎肯定我的用户没有被正确注销,我不明白为什么。 为了login用户,我在路由文件中写了下面的代码 app.post('/api/signup', passport.authenticate('local-signup', function(err, res, req) { console.log(res); })); 然后在另一个文件这个代码来处理我的用户login的逻辑 passport.serializeUser(function(user, done) { console.log("serializeUser"); console.log(user); done(null, user.id); }); passport.deserializeUser(function(id, done) { console.log("deserializeUser"); console.log(id); User.findById(id, function(err, user) { done(err, user); }); }); passport.use('local-signup', new LocalStrategy( { usernameField: 'username', passwordField: 'password', passReqToCallback: true }, function(req, username, password, done) { console.log(req.sessionID); process.nextTick(function() { User.findOne({ 'local.username': username }, […]

Webpack-dev-server找不到要提交的文件

我只是从webpack文档复制粘贴webpack-dev-server + express设置,但是它不起作用,因为服务器找不到要提供的文件。 这个设置有什么问题? server.js const express = require('express'); const webpack = require('webpack'); const webpackDevMiddleware = require('webpack-dev-middleware'); const app = express(); const config = require('../webpack.config.js'); const compiler = webpack(config); app.use(webpackDevMiddleware(compiler, { publicPath: config.output.publicPath })); /*app.use('/',express.static('public')) it works when used insted of webpack dev middleware */ // Serve the files on port 3000. app.listen(3000, function () […]