How to force parse request body as plain text instead of json in Express?

By default bodyParser.text() handles only text/plain. Change the type options to include */json or */*. app.use(‘/some/route’, bodyParser.text({type: ‘*/*’}), function(req, res) { var text = req.body; // I expect text to be a string but it is a JSON }); //or more generally: app.use(bodyParser.text({type:”*/*”})); You can find the docs here

What properties does Node.js express’s Error object expose?

The Error object is actually a native object provided by V8, rather than by node.js or express. The property that will most likely be of the most use to you is stack. E.g., console.log(new Error(‘NotFound’).stack); There are other properties available as well, such as name and message. You can read up on them here. Just … Read more

What is the difference between ‘session’ and ‘cookieSession’ middleware in Connect/Express?

The session middleware implements generic session functionality with in-memory storage by default. It allows you to specify other storage formats, though. The cookieSession middleware, on the other hand, implements cookie-backed storage (that is, the entire session is serialized to the cookie, rather than just a session key. It should really only be used when session … Read more

connect failed: ECONNREFUSED

To access your PC localhost from Android emulator, use 10.0.2.2 instead of 127.0.0.1. localhost or 127.0.0.1 refers to the emulated device itself, not the host the emulator is running on. Reference: https://developer.android.com/studio/run/emulator-networking#networkaddresses For Genymotion use: 10.0.3.2 instead of 10.0.2.2

How to disable Express BodyParser for file uploads (Node.js)

If you need to use the functionality provided by express.bodyParser but you want to disable it for multipart/form-data, the trick is to not use express.bodyParser directly. express.bodyParser is a convenience method that wraps three other methods: express.json, express.urlencoded, and express.multipart. So instead of saying app.use(express.bodyParser()) you just need to say app.use(express.json()) .use(express.urlencoded()) This gives you … Read more

Java URLConnection Timeout

Try this: import java.net.HttpURLConnection; URL url = new URL(“http://www.myurl.com/sample.xml”); HttpURLConnection huc = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(false); huc.setConnectTimeout(15 * 1000); huc.setRequestMethod(“GET”); huc.setRequestProperty(“User-Agent”, “Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)”); huc.connect(); InputStream input = huc.getInputStream(); OR import org.jsoup.nodes.Document; Document doc = null; try { doc = Jsoup.connect(“http://www.myurl.com/sample.xml”).get(); } catch (Exception e) { … Read more

tech