express

Express.js Handle unmached routes

不想你离开。 提交于 2021-02-04 19:57:34
问题 Fellows I develop a Rest API and I want when a route does not exist to send a custom message instead of an html one that express.js sends by default. As fas as I searched I could not find a way to do that. I tried to do: app.all("*",function(req,res){ res.status(404) res.header("Content Type","application/json") res.end(JSON.stringify({message:"Route not found"})) }); But it matches and all already implemented methods. I want only the unmached one to get handled by my app. Edit 1 For each

Convert snake_case to camelCase field names in apollo-server-express

孤街醉人 提交于 2021-02-04 19:01:47
问题 I'm new to GraphQL and Apollo Server, though I have scoured the documentation and Google for an answer. I'm using apollo-server-express to fetch data from a 3rd-party REST API. The REST API uses snake_case for its fields. Is there a simple way or Apollo Server canonical way to convert all resolved field names to camelCase? I'd like to define my types using camel case like: type SomeType { id: ID! createdTime: String updatedTime: String } but the REST API returns object like: { "id": "1234"

How to get the body before uploading file in multer?

a 夏天 提交于 2021-02-04 19:01:08
问题 I'm using multipart/form-data in my NodeJS project which lets the user upload a MP3 and set the settings. I decided to use multer middleware which works fine. https://github.com/expressjs/multer index.ejs <form method="post" action="/acp" role="publish" enctype="multipart/form-data"> <div class="form-group"> <input type="file" name="song" id="song" accept="audio/mpeg"> </div> <input type="checkbox" name="gender" checked data-toggle="toggle" data-on="Male" data-off="Female"> </form> app.js var

How do I set multiple http header fields with the same key in Node.js?

霸气de小男生 提交于 2021-02-04 18:00:05
问题 I'm trying to set up server push with cloudflare, but they require multiple link header fields to push multiple files. However, I can't find any documented way to include multiple header fields with the same key in node.js. I tried providing an array, but that just concatenates them together as the value for a single header field. 回答1: express You pass an array of values to res.header('HeaderName', arrayOfValues) . Here's a working example and cURL output showing the duplicate response

How to set content-type when doing res.send()?

爷,独闯天下 提交于 2021-02-04 14:32:46
问题 At the end of route function, when I do something like this. res.send(JSON.stringify({...})); I would like the content-type of response to be set as "text/plain", but it is "text/html". Is there way to set it explicitly? I search the document but no clue yet. 回答1: setHeader before sending: https://nodejs.org/api/http.html#http_response_setheader_name_value res.setHeader('content-type', 'text/plain'); res.send(JSON.stringify({...})); 回答2: There are a few ways to achieve this, the cleanest

How to set content-type when doing res.send()?

陌路散爱 提交于 2021-02-04 14:32:46
问题 At the end of route function, when I do something like this. res.send(JSON.stringify({...})); I would like the content-type of response to be set as "text/plain", but it is "text/html". Is there way to set it explicitly? I search the document but no clue yet. 回答1: setHeader before sending: https://nodejs.org/api/http.html#http_response_setheader_name_value res.setHeader('content-type', 'text/plain'); res.send(JSON.stringify({...})); 回答2: There are a few ways to achieve this, the cleanest

Disable CORS in Expres.io for socket.io calls

走远了吗. 提交于 2021-02-04 08:09:09
问题 I try to connect from angular to a Express.io socket, but I have error 404 CORS. How can I solve this? XMLHttpRequest cannot load http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1447367208172-29. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. If I load this route directly from browser directly, it works well But from localhost:80 angular to localhot:3000 express.io not works. In my express.io

Disable CORS in Expres.io for socket.io calls

匆匆过客 提交于 2021-02-04 08:08:00
问题 I try to connect from angular to a Express.io socket, but I have error 404 CORS. How can I solve this? XMLHttpRequest cannot load http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1447367208172-29. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. If I load this route directly from browser directly, it works well But from localhost:80 angular to localhot:3000 express.io not works. In my express.io

Concurrently JS application pipeline install and build hangs (Express js for server, Create-React-App for Client)

柔情痞子 提交于 2021-02-02 09:46:47
问题 Problem : I have a project with a server (Express Server that handles file uploading and deleting) and client (Front End Create-React-App). The project structure looks like follows: Root Folder With Server Client Folder Each folder has it's own package.json. Server Package.json. Client package.json I'm trying to build and deploy onto azure however the pipeline hangs on "npm install and build". It seems like the build succeeds but this phase just hangs. Here is my server.js (the routes are not

Can I use a file based database on Heroku?

不问归期 提交于 2021-01-30 09:09:58
问题 I have a small Node.js / Express app deployed to Heroku. I'd like to use a lightweight database like NeDB to persist some data. Is it possible to periodically backup / copy a file from Heroku if I used this approach? 回答1: File-based databases aren't a good fit for Heroku due to its ephemeral filesystem (bold added): Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a