express

I have used “cors” but I found “Access to XMLHttpRequest has been blocked”. Why?

ε祈祈猫儿з 提交于 2021-02-20 03:41:42
问题 I have an express API running and when I make a request I get this message. Error: Access to XMLHttpRequest at 'http://localhost:9000/api/courses' from origin 'http://localhost:4222' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. I am using cors in my API here is my code: import * as express from 'express'; import {Application} from "express"; import {getAllCourses, getCourseById} from "./get-courses.route"; import {searchLessons

I have used “cors” but I found “Access to XMLHttpRequest has been blocked”. Why?

假装没事ソ 提交于 2021-02-20 03:41:27
问题 I have an express API running and when I make a request I get this message. Error: Access to XMLHttpRequest at 'http://localhost:9000/api/courses' from origin 'http://localhost:4222' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. I am using cors in my API here is my code: import * as express from 'express'; import {Application} from "express"; import {getAllCourses, getCourseById} from "./get-courses.route"; import {searchLessons

node.js - Archiving folder using archiver generate an empty zip

佐手、 提交于 2021-02-20 02:59:29
问题 I am trying to archive a folder using archiver, the path of the folder that i'd like to archive look like this : Project | app.js | tmp | folderToArchive │file1.txt │file2.txt │file3.txt my server side code where the zip file will be generated look like this : var archiver = require("archiver"); app.get("/download/:folder", (req, res) => { var FolderName = req.params.folder; var zipName = FolderName + ".zip"; var source = path.join(__dirname, "tmp", FolderName); var out = path.join(__dirname,

node.js - Archiving folder using archiver generate an empty zip

∥☆過路亽.° 提交于 2021-02-20 02:59:07
问题 I am trying to archive a folder using archiver, the path of the folder that i'd like to archive look like this : Project | app.js | tmp | folderToArchive │file1.txt │file2.txt │file3.txt my server side code where the zip file will be generated look like this : var archiver = require("archiver"); app.get("/download/:folder", (req, res) => { var FolderName = req.params.folder; var zipName = FolderName + ".zip"; var source = path.join(__dirname, "tmp", FolderName); var out = path.join(__dirname,

Node.js API with express & mysql - Getting record count, page number, … & provide pagination

醉酒当歌 提交于 2021-02-20 02:53:29
问题 I would like to get and provide the total total_record_count, page_number, page_size, total_pages, has_more information in the response and being able to paginate through the result since I LIMIT the query to 100. What's the best way to do this? That's my current setup: router.get('/?', function(req, res, next) { const sql = "SELECT * from users "; const existingParams = ["title", "active"].filter(field => req.query[field]); if (existingParams.length) { sql += " WHERE "; sql += existingParams

Node.js API with express & mysql - Getting record count, page number, … & provide pagination

白昼怎懂夜的黑 提交于 2021-02-20 02:53:08
问题 I would like to get and provide the total total_record_count, page_number, page_size, total_pages, has_more information in the response and being able to paginate through the result since I LIMIT the query to 100. What's the best way to do this? That's my current setup: router.get('/?', function(req, res, next) { const sql = "SELECT * from users "; const existingParams = ["title", "active"].filter(field => req.query[field]); if (existingParams.length) { sql += " WHERE "; sql += existingParams

Hot reloading with express and chokidar causes a http headers sent error when using multiple routes

只愿长相守 提交于 2021-02-19 15:54:59
问题 I've been trying a variety of setups for hot reloading and one that I've come across is the https://github.com/glenjamin/ultimate-hot-reloading-example/. Modifying this boilerplate code as a starting point, I've come across the following problem in my server code: // server.js import chokidar from 'chokidar'; import express from 'express'; const app = express(); // this is the middleware for handline all of my routes app.use(function (req, res, next) { require('./server/index')(req, res, next

node ejs reference error data not defined at eval when handing data to view

前提是你 提交于 2021-02-19 07:54:06
问题 i've been closing in on a node application using express and ejs, but when i try to hand data to my view from the controller like so var myData = { theData: data }; res.render(path.join(__dirname + '/../views/index'), myData); i get a nice error ReferenceError:.. myData is not defined eval (from ejs lib) when trying to access myData in the view like so var data = <%-myData%>; or in any other way basically, i've tried stringifying the data, wrapping it in another object and stuff like that but

Node.js binary to PDF

筅森魡賤 提交于 2021-02-19 06:48:19
问题 I have got a express server, which creates a pdf file. I am trying to send this file to the client: const fs = require('fs'); function download(req, res) { var filePath = '/../../myPdf.pdf'; fs.readFile(__dirname + filePath, function(err, data) { if (err) throw new Error(err); console.log('yeyy, no errors :)'); if (!data) throw new Error('Expected data, but got', data); console.log('got data', data); res.contentType('application/pdf'); res.send(data); }); } On the client I want to download it

Node.js binary to PDF

假如想象 提交于 2021-02-19 06:48:13
问题 I have got a express server, which creates a pdf file. I am trying to send this file to the client: const fs = require('fs'); function download(req, res) { var filePath = '/../../myPdf.pdf'; fs.readFile(__dirname + filePath, function(err, data) { if (err) throw new Error(err); console.log('yeyy, no errors :)'); if (!data) throw new Error('Expected data, but got', data); console.log('got data', data); res.contentType('application/pdf'); res.send(data); }); } On the client I want to download it