I have this simple node.js static file server:
var express = require('express');
var app = express();
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: "secret" }));
app.use(express.static(process.env.OPENSHIFT_REPO_DIR + '/public' ));
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
app.listen(port, ipaddress);
When I publish to OpenShift (no errors in the terminal) and navigate to the site, I see Cannot GET /
I have index.html in my public folder, along with some css and JavaScript.
Any idea what I'm doing wrong?
Here it is: Hey, did you make sure to commit the public folder?
You need the load the routes.
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
You are also not loading any views.
Here's the documentation of express: http://expressjs.com/guide.html
Related
I'm trying to setup a basic mean stack by following this guide, but the client doesn't seem to render the app instead the body contains,
<body>
<app-root></app-root>
</body>
The file structure is exactly the same as a blank angular cli project except the addition of two extra files.
PLUS: npm install --save ejs cors express body-parser
routes/index.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('index.html');
});
module.exports = router;
server.js
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var cors = require('cors')
var index = require('./routes/index');
// app
var app = express();
// cors
app.use(cors());
// views
app.set('views', path.join(__dirname, 'src'));
// engine
app.set('view enginer', 'ejs');
app.engine('html', require('ejs').renderFile);
// angular dist
app.use(express.static(__dirname + '/dist'));
// body bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// route
app.use('/', index);
// Initialize the app.
var server = app.listen(process.env.PORT || 3000, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
I run an ng build and node server.js but get a blank white page in the browser.
Perhaps there is a breaking change that I'm not aware of since that guide was using angular2 (I'm using angular 6).
Since your index.html isn't directly inside the dist folder (rather, it is inside a sub-folder for some reason), try changing app.use(express.static(__dirname + '/dist')); to app.use(express.static(__dirname + '/dist/<your project name here>'));
I have a server running on Node Js. What I'm doing is whenever the users submit something to save their input to a text file. When I run my server as a localhost it works and saves the input to the file. Whenever I run it on the real published server it doesn't. Is there a way to accomplish it on the real server without a database?
My code:
var fs = require('fs');
const log=require('simple-node-logger').createSimpleLogger();
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var port = process.env.PORT || 8081;
app.use(express.static(__dirname + '/server'));
app.use(express.static(__dirname + '/public'));
app.use('/images', express.static(__dirname +'/images'));
app.get('/', function(req, res){
res.sendFile('main.html');
});
app.listen(port, function(){
console.log('Server is running on port:' + port);
});
app.post('/submit', function(req, res){
var data = fs.writeFileSync('fileSync', req.body.rank, 'utf8');
return res.sendFile('success.html');
});
Thank you in advance!
I'm new in Angularjs and i'm trying to create a basic one page app using ngRoute. But i have a problem. I wan't my user to get the index.html file everytime. But if the url is /page1 my server is returning the page1 so index.html isn't loading. I tryed to fix it by doing like that :
Angular file :
test.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl : '/page1',
controller : 'trapController'
})
.when('/test', {
templateUrl : '/page2',
controller : 'trapController'
});
$locationProvider.html5Mode(true);
});
Node.js file :
require('dotenv').load();
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
const bodyParser = require('body-parser');
const path = require('path');
const apirouter = require('./apirouter');
const viewrouter = require('./viewrouter');
app.use('/api', apirouter);
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.engine('html', require('ejs').renderFile);
app.use('/*', function(req, res){
res.sendFile(__dirname + '/public/index.html');
});
// START THE SERVER
// =============================================================================
app.listen(port, () => {
console.log('Server running on port ' + port);
});
But this way nothing is working.. It seems like every call is asking to the server and not looking into the public folder. So when i call for a .js file it load me a new index.html file... I don't know how to do.. Can you help me ? :)
I was apparently doing wrong with :
Better use this :
app.use('/', express.static(__dirname + '/public'));
Than this :
app.use(express.static(path.join(__dirname, 'public')));
I have the following code which is defaulting to use HTTPS, I'm just unclear how to send an index.html file back
var express = require('express'),
path = require('path'),
fs = require('fs'),
app = express(),
staticRoot = __dirname + '/',
httpsRedirect = require('express-https-redirect');
app.set('port', (process.env.PORT || 3001));
app.use('/', httpsRedirect())
app.listen(app.get('port'), function () {
console.log('app running on port', app.get('port'));
});
Any help is greatly appreciated
You can send the html file usually asfollows
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
Note: express-https-redirect middleware redirects non-secure access to HTTPS Only.
I have a node.js server with express Framework.
var express = require('express');
var http = require('http');
var api_helper = require('./helpers/api_helper');
var app = express();
app.use(app.router);
app.use(express.bodyParser());
app.set('port', process.env.PORT || 8081);
app.post('/api/nodev1/users/login', function(req, res){
var email = req.param('email', null);
var password = req.param('password', null);
console.log(email);console.log(password);
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
When I try to post a request to /api/nodev1/users/login I cannot read the parameters. I am trying with curl as follows:
curl -d "email=user#example2.com" -d "password=mypassword" http://localhost:8081/api/nodev1/users/login
The email and password are undefined.
You have to move app.use(app.router) below app.use(express.bodyParser()). app.router is just a hook in which stage to handle your routes. And if it comes before bodyParser the body is not parsed.
Your code could look like this (in case I didn't manage to explain understandable):
var app = express();
app.use(express.bodyParser());
app.set('port', process.env.PORT || 8081);
app.use(app.router);
// I added following line so you can better see what happens
app.use(express.logger('dev'));
app.post('/api/nodev1/users/login', function(req, res){ ... }
Offtopic remark: express.bodyParser() should only be used when you have file-uploads. And then you have to take care of deleting temp-files. If you don't have file-uploads, you are better off with only
app.use(express.json());
app.use(express.urlencoded());
I just wanted to add this in case you didn't know. I ran in problems because I didn't know...
Edit for Express 4
Thanks to #jonathan-ong there is no app.use(app.router) since Express 4:
All routing methods will be added in the order in which they appear. You should not do app.use(app.router). This eliminates the most common issue with Express.
In other words, mixing app.use() and app[VERB]() will work exactly in the order in which they are called.
Read more: New features in 4.x.
edit - nope, see other answer about middleware order!
change req.param to req.body.x:
app.post('/api/nodev1/users/login', function(req, res){
var email = req.param('email', null);
var password = req.param('password', null);
console.log(email);console.log(password);
});
to
app.post('/api/nodev1/users/login', function(req, res){
var email = req.body.email);
var password = req.body.password);
console.log(email); console.log(password);
});