Node, Express, sapui5 app Initial load of metadata failed - node.js

I Have the following code.
var express = require('express');
var fs = require('fs');
var cors = require('cors');
var app = express();
app.use(cors())
app.set('port', (process.env.PORT || 8083));
app.use(express.static(__dirname + '/project1/webapp'));
app.get('/', function(request, response) {
fs.readFile('./project1/webapp/index.html', function(err, data){
response.send(data.toString());
});
});
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'));
});
And it is calling the app and works so far. Problem is, I get in the network tab.
Initial load of metadata failed. So odata service is not called.
I think I have to include a proxy but have no Idea how.
Does anybody know how to handle this?
Rg. Joerg

Related

AWS Deploy my NodeJS express app

The following is my ./server.js my angular dist is at ./client/dist when I node server.js in the terminal my angular app and nodejs backend works as expected. Now how do I deploy on aws beanstalk (im open to changing beanstalk)?
Most tutorials want me to start the job from scratch but i really need the server to work as shown below like it does on localhost.
const express = require('express');
const colors = require('colors');
const bodyParser = require('body-parser');
const compression = require('compression');
const path = require('path');
const fs = require('fs');
const cors = require('cors');
// init "app"
const app = express();
var staticRoot = __dirname + '/client/dist/';
app.set('port', (process.env.PORT || 5000));
app.use(cors({origin: `http://localhost:4200`}));
//parse incoming data before routes
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// api routes
app.use('/api',require('./api/api'));
app.use(function(req, res, next) {
//if the request is not html then move along
var accept = req.accepts('html', 'json', 'xml');
if (accept !== 'html') {
return next();
}
// if the request has a '.' assume that it's for a file, move along
var ext = path.extname(req.path);
if (ext !== '') {
return next();
}
fs.createReadStream(staticRoot + 'index.html').pipe(res);
});
app.use(express.static(staticRoot));
app.listen(app.get('port'), function() {
console.log('app running on port', app.get('port'));
});
I created a modern MEAN Stack guide with full tutorials and source code. For your question in particular, I created a step-by-step guide on how to deploy a MEAN stack app to AWS Elastic Beanstalk (https://www.meankit.io/guides/deploy-with-aws)
There's also reference links as well if you need further information.

access application port : express

I am trying to build a simple chat application with node.js using Express.My node version is v0.12.7 and express version is 4.13.1.
I need to access the port the application is listening to but I am not getting how to do that even after lot of research over google.
My code in index.js is:
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
io.on('connection', function(socket){
console.log('a user connected');
});
console.log(app.get('port')); // not working
http.listen(app.get('port'), function(){
console.info('Server listening on port :');
});
module.exports = router;
Please suggest If this can be done and how. Thanks in advance :)
In order to get the port, you're going to have to set it first like this
var app = require('express')();
app.set('port', process.env.PORT || 3000);
...

Access the public folder when using node and Express?

I have looked through stackoverflow and read the express documentation, I can't figure out why the app won't run when I implement "app.use(express.static());" does anyone know a fix?
var express = require('express')();
var app = require('express')();
var server = require('http').Server(app);
var io = require("socket.io").listen(server);
//If i use this my app will not start
// app.use(express.static());
app.get('/', function(req, res){
res.sendfile('index.html');
});
//Get input from front-end
io.on('connection', function(socket){
// On input do something
socket.on('directional in', function(unique_id, input, input1){
// send info to index
io.emit('directional out', unique_id, input, input1);
});
});
server.listen(3000, function(){
// Server is running
console.log('listening on *:3000');
});
Any help would be great!
You're not initialising express correctly. The correct way would be as follows:
var express = require('express');
var app = express();
With this, you will be able to do
app.use(express.static(__dirname + '/public'));
All together, a fully functional express app would look like this in its most basic form:
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.listen(process.env.PORT || 3000);
Let me know what happens.

Not able to solve Express Middleware issue

My first piece of code below:
var express = require('express');
var app = express.createServer(express.logger());
app.get('/', function(request, response){
response.send('Hello World 2');
});
var port = process.env.PORT || 5000;
app.listen(port, function(){
console.log("Listening on " + port);
});
Threw up the error: "Most middleware (like logger) is no longer bundled with Express and must be installed separately"
So I looked at StackOverflow, did npm install morgan, and changed my code to:
var express = require('express');
var logger = require('morgan');
var app = express.createServer(logger());
app.get('/', function(request, response){
response.send('Hello World 2');
});
var port = process.env.PORT || 5000;
app.listen(port, function(){
console.log("Listening on " + port);
});
Now I get this error:
var app = express.createServer(logger());
^
TypeError: Object function createApplication() {
var app = function(req, res, next) {
app.handle(req, res, next);
};
mixin(app, proto);
mixin(app, EventEmitter.prototype);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
} has no method 'createServer'
That is because createServer method has been removed from express.
use
app = express();
app.use(logger())
instead of
app = express.createServer(logger())
Many things got changed from express 3.0 to 4.0. You should have a look here
You should create app with express(). Afterwards, you can setup any middleware (like morgan in this case) with app.use:
var express = require('express');
var logger = require('morgan');
var app = express();
app.use(logger());
...
app.use() documentation: http://expressjs.com/4x/api.html#app.use. (Linking to Express 4.x documentation as not explicit what Express version you're running).
There is an exact same example like the one I wrote above in Morgan's README in GitHub.

how to print the data from post request on console

I am trying to print the post data on my console
app.js
var express = require('express')
, http = require('http');
var app = express();
app.set('port', process.env.PORT || 7002);
app.use(express.static(__dirname + '/public/images'));
app.post('/Details/',function(request,response,next){
app.use(express.bodyParser());
var keyName=request.query.Key;
console.log(keyName);
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Initial snapshot::
I test with POST-MAN with below data::
Now i get error as::
I just want to print the data i recieved from postman that is dev
..... which is being displayed as undefined ?
How to resolve this !
[Edit] ---- Adding body parser outside the route
var express = require('express')
, http = require('http');
var app = express();
app.set('port', process.env.PORT || 7002);
app.use(express.static(__dirname + '/public/images'));
app.use(express.bodyParser());
app.post('/Details/',function(request,response,next){
var keyName=request.query.Key;
console.log(keyName);
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Still have same error
Instead of query:
var keyName=request.query.Key;
console.log(keyName);
use body:
var keyName1=request.body.key;
console.log(keyName1);
Code:
var express = require('express')
, async = require('async')
, http = require('http');
var app = express();
app.set('port', process.env.PORT || 7002);
app.use(express.static(__dirname + '/public/images'));
app.use(express.bodyParser());
app.post('/Details/',function(request,response,next){
var keyName1=request.body.key;
console.log(keyName1);
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var express = require('express');
var app = express();
app.use(express.bodyParser());
app.post('/post/', function(req, res) {
// print to console
console.log(req.body);
// just call res.end(), or show as string on web
res.send(JSON.stringify(req.body, null, 4));
});
app.listen(7002);
Use request.query when you have querystring params.
For form/post data use req.body.
In your case, use request.body.key.
An update on using the middleware, body-parser, for later versions of Express: Using app.use(express.bodyParser()) will report an error such as:
Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
This can be addressed by first installing the body-parser middleware:
npm install body-parser
then write code such as:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
and then accessing the body of the request object, for example, console.log(req.body)
You can't call app.use(express.bodyParser()); inside middleware/route handler:
request should pass through bodyParser() before it reaches route handler
you will be adding new bodyParser()s in each request, but they will be after app.router and will never work
Use built in function "util" to print any type of json data in express js
var util = require("util");
console.log(util.inspect(myObject, {showHidden: false, depth: null}));

Resources