In this fictive project I am trying to set up the Routing structure. Although I used module.exports in both files, running a test still gives me the following error:
TypeError: Router.use() requires a middleware function but got a Object
My Code:
Minions.js
const minionsRouter = require('express').Router();
module.exports = minionsRouter;
const {
getAllFromDatabase,
addToDatabase,
getFromDatabaseById,
updateInstanceInDatabase,
deleteFromDatabasebyId,
} = require('./db.js');
minionsRouter.get('/', (req, res, next) => {
const minionsArray = getAllFromDatabase('minions');
if (minionsArray) {
res.status(200).send(minionsArray);
} else {
res.status(404).send();
}
});
API.js
const express = require('express');
const apiRouter = express.Router();
const minionsRouter = require('./minions');
const ideasRouter = require('./ideas');
const meetingsRouter = require('./meetings');
apiRouter.use('/minions', minionsRouter);
apiRouter.use('/ideas', ideasRouter);
apiRouter.use('/meetings', meetingsRouter);
module.exports = apiRouter;
Server.js
const express = require('express');
const app = express();
module.exports = app;
/* Do not change the following line! It is required for testing and allowing
* the frontend application to interact as planned with the api server
*/
const PORT = process.env.PORT || 4001;
// Add middleware for handling CORS requests from index.html
const cors = require('cors');
app.use(cors());
// Add middleware for parsing request bodies here:
const bodyParser = require('body-parser');
app.use(bodyParser.json());
// Mount your existing apiRouter below at the '/api' path.
const apiRouter = require('./server/api');
app.use('/api', apiRouter);
// This conditional is here for testing purposes:
if (!module.parent) {
// Add your code to start the server listening at PORT below:
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`)
})
};
Any help is much appreciated!
I read a tutorial about export/require in node/express and wonder if someone could explain to me with my example:
why is my routing in app.js not working? What would I have to export and require to make it work? Thanks a lot!
index.js:
'use strict'
var express = require('express');
var app = express();
module.exports = app;
var PORT = process.env.PORT || 1337;
app.listen(PORT, function() {
console.log('Server is listening!');
})
app.js:
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var app = express();
//var app=require('./index.js');
module.exports = function() {
app.use(express.static(path.join(__dirname, '../public')));
app.use(express.static(path.join(__dirname, '../browser')));
}
Your example will not work correctly at least because you declared two separate express servers in both files var app = express().
This code is not the most suitable for practice in exporting/requiring because such servers initializations are usually placed in one file.
But anyway, if you would like to use this example and make it work, let's do in in this way:
./ index.js
'use strict'
var express = require('express');
var setupServing = require('./setupServing.js'); //import our function
var PORT = process.env.PORT || 1337;
var app = express(); //create express app
setupServing(app); //call imported function to config our app
app.listen(PORT, function() { //start
console.log('Server is listening!');
})
./ setupServing.js
var express = require('express');
var path = require('path');
//export function which can configure static serve for app
module.exports = function(app) { //take app as an argument
app.use(express.static(path.join(__dirname, '../public')));
}
Note that this example still is not logical enough and I don't think you will face such code in any real project, but anyway it will work and demonstrate exporting/requiring in nodejs.
I am working on a express project where subdomains are used.
Now the problem i am facing is that my domain name is ("companyName.co.in") and my node JS code is considering companyName as the subdomain.
Here is my code:
server.js
var express = require('express');
var app = express();
var subdomain = require('express-subdomain');
var bodyParser = require('body-parser');
var path = require('path');
var expressValidator = require('express-validator');
var expressJwt = require('express-jwt');
var jwt = require('jsonwebtoken');
var cors = require('cors');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(cors());
//app.use(cookieParser());
app.use(express.static('../app'));
app.use(express.static('static/'));
app.use('/api/user',require('./controllers/user.controller'));
var server = app.listen(3000,function(){
console.log('Server listening at 3000');
});
user.controller.js
var express = require('express');
var router = express.Router();
router.get("/details", getUserDetails);
function getUserDetails(req, res) {
var subdomain = (req.subdomains.length > 0) ? req.subdomains[0] : '';
console.log(subdomain)
}
module.exports = router;
So when i call the api/user/details i find that it gives the companyName as the subdomain.
like: requesting from "companyName.co.in"
subdomain should be null but it gives me subdomain as companyName.
Can you please help how can i let the node server know that "companyName.co.in" this is the host and also allow "*.companyName.co.in" where the subdomain can be anything.
You can solve this in your case by setting the 'subdomain offset' express app setting to 3.
http://expressjs.com/en/4x/api.html#app.settings.table
var express = require('express');
var app = express();
app.set('subdomain offset', 3);
I'm just trying to run sample codes from Developing Microservices with Node js, and it says:
var express = require('express')
var bodyParser = require('body-parser')
var cookieParser = require('cookie-parser')
var methodOverride = require('method-override')
var seneca = require('seneca')()
var argv = require('optimist').argv
var app = express()
var cors = require('cors')
var routes = require('./../routes/index')
let path = require('path')
var webpack = require('webpack')
var webpackMiddleware = require('webpack-dev-middleware')
var config = require('./../webpack.config.js')
var compiler = webpack(config)
var conf = {
port: argv.p || 7770
}
app.engine('jsx', require('express-react-views').createEngine())
app.set('port', conf.port)
app.use(cors())
app.use('/public', express.static(path.join(__dirname,'./../public')))
app.use('/views', express.static(path.join(__dirname, './../views')))
app.use(webpackMiddleware(compiler));
app.use(cookieParser())
app.use(express.query())
app.use(bodyParser.urlencoded({extended: true}))
app.use(methodOverride())
app.use(bodyParser.json())
app.use(express.static('public'))
app.use(seneca.export('web')) // Error line
seneca.use('./../lib/registerAPI')
app.use('/', routes)
module.exports = app
but Im getting an error that says:
/home/quocdinh/workspace/ECommerce/ass-ECommerce/node_modules/express/lib/application.js:177
throw new TypeError('app.use() requires middleware functions');
^
TypeError: app.use() requires middleware functions
at EventEmitter.use (/home/quocdinh/workspace/ECommerce/ass-ECommerce/node_modules/express/lib/application.js:177:11)
at Object.<anonymous> (/home/quocdinh/workspace/ECommerce/ass-ECommerce/src/app.js:33:5) // --> line: app.use(seneca.export('web'))
I have tried to find solutions but ineffective.
I tried adding
app.use(require('seneca-web'))
but still not be
I tried to lower the version of the node version that I have to 4.0 from 6.0, but still got the same error
I'm learning microservices with seneca too.
Using express version ~4.13.4 and seneca version ^2.0.0 works for me.
More information can be found in seneca getting started examples at this URL:
https://github.com/senecajs-attic/getting-started
Look at their package.json file. Hope it helps!
Can I create an Express server listening on both HTTP and HTTPS, with the same routes and the same middlewares?
Currently I do this with Express on HTTP, with stunnel tunneling HTTPS to Express, but I prefer a pure Node solution.
I can do it with this code, but using the handle method that is marked as private:
var express = require( 'express' )
, https = require("https")
, fs = require( 'fs' );
var app = express.createServer();
// init routes and middlewares
app.listen( 80 );
var privateKey = fs.readFileSync( 'privatekey.pem' ).toString();
var certificate = fs.readFileSync( 'certificate.pem' ).toString();
var options = {key: privateKey, cert: certificate};
https.createServer( options, function(req,res)
{
app.handle( req, res );
} ).listen( 443 );
To enable your app to listen for both http and https on ports 80 and 443 respectively, do the following
Create an express app:
var express = require('express');
var app = express();
The app returned by express() is a JavaScript function. It can be be passed to Node’s HTTP servers as a callback to handle requests. This makes it easy to provide both HTTP and HTTPS versions of your app using the same code base.
You can do so as follows:
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var app = express();
var options = {
key: fs.readFileSync('/path/to/key.pem'),
cert: fs.readFileSync('/path/to/cert.pem'),
ca: fs.readFileSync('/path/to/ca.pem')
};
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
For complete detail see the doc
As a possible update to this question, you might want to check out the changes here for express 3. The change document says:
The return value of express() is a JavaScript Function,
encapsulating everything that makes an Express app tick. This means
you can easily setup HTTP and HTTPS versions of your application by
passing it to node's http.createServer() and https.createServer():
In Express 3, express.createServer() is now express()
Here is a complete example for express 3:
var fs = require('fs')
, https = require('https')
, http = require('http')
, express = require('express')
, keys_dir = 'keys/'
, server_options = {
key : fs.readFileSync(keys_dir + 'privatekey.pem'),
ca : fs.readFileSync(keys_dir + 'certauthority.pem'),
cert : fs.readFileSync(keys_dir + 'certificate.pem')
}
, app = express();
app.configure(function(){
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session( { secret: '' } ));
app.use(app.router);
});
app.configure('development',function(){
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({dumpExceptions: true, showStack:true}));
app.set('view options', { pretty: true });
});
app.get('/', function(req, res){
res.send('Hello World!');
});
https.createServer(server_options,app).listen(7000);
http.createServer(app).listen(8000);
You can share the implementation via something like:
var register = function (app) {
// config middleware
app.configure({
});
// config routes
app.get(...);
};
var http = express.createServer();
register(http);
http.listen(80);
var https = express.createServer({ key: /* https properties */ });
register(https);
https.listen(443);
You can use express and https in same port.
this works for me.
const express=require('express');
const app=express();
const cors=require('cors');
const path=require("path");
const routes=require('./routes/api');
const routerComplain=require('./routes/api');
const routerstores=require('./routes/api');
const routerstock=require('./routes/api');
const routerreport=require('./routes/api');
const routeritem=require('./routes/api');
const bodyParser=require('body-parser');
const routerRegister=require('./routes/api');
const mongoose=require('mongoose');
var server = require('http').Server(app);
var io = require('socket.io')(server);
require("dotenv").config();
mongoose.connect('mongodb://#################',{ useNewUrlParser: true },(err)=>{
if(!err){
console.log('db connected')
}else{
console.log('error in db')
}
});
mongoose.Promise = global.Promise;
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(cors({credentials: true, origin:'http://localhost:3000'}));
app.use(express.static(path.join(__dirname, "client", "build")))
app.use('/reg',routes);
app.use('/complain',routerComplain);
app.use('/register',routerRegister);
app.use('/stores',routerstores);
app.use('/reports',routerreport);
app.use('/stock',routerstock);
app.use('/items',routeritem);
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
})
const port = process.env.port||4000;
server.listen(port,function(){
console.log('now listening for request');
});
If you want to use the traditional two ports, one of the above solutions probably works, but using httpolyglot, you can really easily have http and https on the same port with the same middlewares.
https://github.com/mscdex/httpolyglot
Here's some skeleton code that worked for me:
var express = require('express');
var fs = require('fs');
var httpolyglot = require('httpolyglot');
var app = express();
const options = {
key: fs.readFileSync("/etc/ssl/certs/key"),
cert: fs.readFileSync("/etc/ssl/certs/cer.cer")
};
httpolyglot.createServer(options, app).listen(port);
and if you want http -> https forwarding, you can just add this middleware function before the createServer() call:
app.use(function(req, res, next) {
if (!req.secure ) {
res.redirect (301, 'https://' + req.hostname + ':port' + req.originalUrl);
}
next();
});
This can be set up on a custom port
Similar post
Can I configure expressjs to serve some pages over http and others over https?
Be aware that express now support creating Https servers with:
var app = require('express').createServer({ key: ... });