Webpack compiles successfully and putting 127.0.0.1:8080 in the address bar
brings me to the index.html.
On index.html there is a button that when clicked makes a GET request to an
endpoint 127.0.0.1:8080/ping.
The request is made, however there is nothing in index.js to handle the
request, and the request obviously fails with a 404.
The application uses webpack. The config file is included, although I am
not sure it is relevant.
How do I handle the request in the back-end without adding any dependencies?
1. index.js:
const MainTemplate = require('./main.njk');
document.getElementById('content').innerHTML = MainTemplate.render();
2. index.html:
<!DOCTYPE html>
<html>
<head></head>
<body>
<button id="button" onclick="make_ajax_call()">Ajax Call</button>
<script src="bundle.js"></script>
</body>
</html>
<script>
function make_ajax_cal() {
var http_request = new XMLHttpRequest();
http_request.open('GET', 'http://127.0.0.1:8080/ping');
http_request.send();
http_request.onreadystatechange = function () {
if (http_request.readyState === 4 && http_request.status === 200) {
console.log("Response received");
}
else if (http_request.status === 404) {
console.log("404 not found");
}
};
}
</script>
3. webpack.config.js:
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
entry: [
'./src/index'
],
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
plugins: [
new CopyWebpackPlugin([
{ from: 'src' },
{ from: 'img' }
])
],
module: {
loaders: [
{
test: /\.njk$/,
loader: 'nunjucks-loader'
},
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: 'babel',
query: {
presets: ['es2015']
}
}
resolve: {
modulesDirectories: [
"",
"src",
"node_modules"
]
}
}
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/ef2b794b-5b20-45b9-afcf-390f3b5d0c06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.