express

Node.js/Express redirect all to angular 2 page

拥有回忆 提交于 2019-12-31 03:22:27
问题 I am creating an Angular 2 app with Node.js and Express. The problem I am having is that my routes file doesnt work with a wildcard. Everytime I visit the page with anything other then / (for example /test ) it says the following: ReferenceError: path is not defined My server.js: const express = require('express'); const app = express(); const path = require('path'); const routes = require('./routes'); const data = require('./articles.json'); app.use(express.static(path.join(__dirname, '/dist

apollo-server-express CORS issue

大兔子大兔子 提交于 2019-12-31 01:01:28
问题 So I am migrating to apollo-server-express 2.3.3 ( I was using 1.3.6 ) I've followed several guides, making the necessary tweaks but am stuck in a CORS issue. According to the docs you have to use the applyMiddleware function to wire up the apollo server with express. I am currently doing the following: const app = express(); // CORS configuration const corsOptions = { origin: 'http://localhost:3000', credentials: true } app.use(cors(corsOptions)) // Setup JWT authentication middleware app

how to fix [Object: null prototype] { title: 'product' }

流过昼夜 提交于 2019-12-30 18:48:31
问题 I've started learning node.js with express framework , when I post a form like this : router.get('/add-product',(req,res,next)=>{ res.send('<form action="/product" method="POST" ><input type="text" name="title" /><button type="submit">Submit</button></form>'); }); router.post('/product',(req,res,next)=>{ console.log(req.body); res.redirect('/'); }); When I do console.log(req.body) it displays: [Object: null prototype] { title: 'product' } instead of just { title: 'product' } I'm wondering if

Mongoose Schema Error: “Cast to string failed for value” when pushing object to empty array

孤人 提交于 2019-12-30 17:32:08
问题 I have a strange problem and cannot figure out what the problem is. The Error-message doesn't help. I'm sending an "alarm" to the server and want to save this alarm to my "device" which already exist in the database. The alarm object I send to the server looks like this: {actionTaken: "none", dateTime: "20152111191512", difference: 4.88, timestamp: 1448128894781} The Schema for the device is as follows: var deviceSchema = new Schema({ deviceId: { type : String, index : { unique : true,

Query and Insert with a single command

霸气de小男生 提交于 2019-12-30 14:44:11
问题 I have the following documents: { "_id": "538584aad48c6cdc3f07a2b3", "startTime": "2014-06-12T21:30:00.000Z", "endTime": "2014-06-12T22:00:00.000Z", }, { "_id": "538584b1d48c6cdc3f07a2b4", "startTime": "2014-06-12T22:30:00.000Z", "endTime": "2014-06-12T23:00:00.000Z", } All of them have startTime and endTime value. I need to maintain consistency that no two date spans in the collection overlap. Let's say if I add the following document with the following dates: db.collection.insert({

Ajax post response from express js keeps throwing error

百般思念 提交于 2019-12-30 11:48:27
问题 I am having a real hard time standing up bidirectional data transfer from a html page to my node.js application and then back to the html page. I'm pretty sure I'm all over the correct solution, but I'm just not getting it to work. I'm using the following for my node.js application: var express = require('/usr/lib/node_modules/express'); var app = express(); app.use(function(err, req, res, next){ console.error(err.stack); res.send(500, 'Something broke!'); }); app.use(express.bodyParser());

Sequelize - SQL Server - order by for association tables

拜拜、爱过 提交于 2019-12-30 10:37:21
问题 I have 3 tables such as user , userProfile and userProfileImages . User is mapping with userPrfoile as has many and userProfile is mapping with userProfileImages as has many. I need to write the order by query in userProfileImages , I tried as below, but no luck. User.findById(uID, { include: [ model: sequelize.models.userProfile as: userProfile, include: [ { model: sequelize.models.userProfileImages, as: 'profileImages', } ], order: [[sequelize.models.userProfileImages.id, "desc"]] // order:

Express' render/redirect does not work if the call isn't coming from a submit method in a form

自作多情 提交于 2019-12-30 10:08:12
问题 Task Perform a POST request from a JS method, so that variable values can be sent as parameters. Environment NodeJS Express BodyParser ejs My first attempt Frontend: <html> <head> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js'></script> <script type="text/javascript"> function postAnswer() { $.post('vote', { message: "hello!"}, function(returnedData) { console.log("Post returned data: " + returnedData); }); } </script> </head> <body> <button id='send' onClick=

proxy json requests with node express

纵饮孤独 提交于 2019-12-30 09:46:10
问题 I use the following node-express code to proxy requests from a web server to an API server: app.use('/api', function(req, res) { var url = 'http://my.domain.com/api' + req.url; req.pipe(request(url)).pipe(res); }); This works well for simple requests of any verb (get, post, etc...), however once I send 'Content-type': 'application/json' requests, it hangs on the pipe line. Why does this simple node-express proxy code hang on json requests? How can it be altered to support them? 回答1: You need

Finding an Embedded Document by a specific property in Mongoose, Node.js, MongodDB

我的梦境 提交于 2019-12-30 09:05:07
问题 For this app, I'm using Node.js, MongoDB, Mongoose & Express So I have a Param Object that contains an array of Pivots, and I want to read certain data from the pivots as outlined below ---in models.js------------------------- var Pivot = new Schema({ value : String , destination : String , counter : Number }); var Param = new Schema({ title : String , desc : String , pivots : [Pivot] }); ------------- in main.js -------------- var Param = db.model('Param'); app.get('/:title/:value', function