- example of configuration of webpack with express and routes for REST-API
- example project can be found here
Last active
June 16, 2022 03:48
-
-
Save isabolic/938523889a44182a0c3ceab2eac6bc44 to your computer and use it in GitHub Desktop.
webpack configuration with server side (REST-api)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "webpack-demo", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "index.js" | |
}, | |
"author": "isabolic99", | |
"license": "MIT", | |
"devDependencies": { | |
"html-webpack-plugin": "^2.29.0", | |
"path": "^0.12.7", | |
"webpack": "^3.4.1", | |
"webpack-dev-server": "^2.6.1" | |
}, | |
"dependencies": { | |
"body-parser": "^1.17.2", | |
"express": "^4.15.3", | |
"routes": "^2.1.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function() { | |
const Webpack = require("webpack"); | |
const WebpackDevServer = require('webpack-dev-server'); | |
const webpackConfig = require("webpack.config.js"); | |
const express = require("express"); | |
const routes = require("routes"); | |
const bodyParser = require("body-parser"); | |
// load from webpack.config.js | |
const compiler = Webpack(webpackConfig); | |
const server = new WebpackDevServer(compiler, { | |
stats: { | |
colors: true | |
} | |
}); | |
// create exrepss router | |
var app = express.Router(); | |
// examples of url path to get details | |
// http://localhost:8080/API/134/ | |
app.post("/:id", (req, res) => { | |
if (!(req.params.id )) { | |
res.send(500, "Missing ID parameter."); | |
} else { | |
res.json({"successful":true}); | |
} | |
}); | |
// examples of url path to remove | |
// http://localhost:8080/API/134/remove | |
app.post("/:id/remove", (req, res) => { | |
if (!(req.params.id )) { | |
res.send(500, "Missing ID parameter."); | |
} else { | |
res.json({"successful":true}); | |
} | |
}); | |
// examples of url path to get list | |
// http://localhost:8080/API/ | |
app.get("/", (req, res) => { | |
res.json(storeFavorite); | |
}); | |
// define parent url path | |
server.use("/API", app); | |
server.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
server.use(bodyParser.json()); | |
server.listen(8080, "localhost", () => { | |
console.log("Starting server on http://localhost:8080"); | |
}); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const HtmlWebpackPlugin = require('html-webpack-plugin'); | |
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({ | |
template: './client/index.html', | |
filename: 'index.html', | |
inject: 'body' | |
}) | |
var assetsPath = path.join(__dirname, "public", "assets"); | |
var publicPath = "/"; | |
module.exports = { | |
// The configuration for the client | |
name: 'client', | |
entry: './client/index.js', | |
output: { | |
path: path.resolve('dist'), | |
filename: 'index_bundle.js', | |
publicPath: publicPath | |
}, | |
module: { | |
loaders: [ | |
{ test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ }, | |
{ test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ } | |
] | |
}, | |
plugins: [HtmlWebpackPluginConfig] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment