I am new in nodejs,In server (using ssh) i installed node (expressjs) and npm on server successfully,
In app.js i put following code
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('<html><body><h1>Hello World</h1></body></html>');
});
var server = app.listen(3000, function () {
console.log('Node server is running..');
});
In xshell (command prompt) displaying message
"ubuntu#ip-***-**-*-***:~/admin/$ node app.js
Node server is running.."
Now i want to see Hello world in browser,How can i do this ?
For example my hostname is 11.111.11.111
and ip (displaying by xshell) is 222-22-2-222
And in ftp i put my code inside following directory
/home/ubuntu/admin
I just want to know that how can i see output in browser ? I tried with following urls but not worked for me
(hostname)
**.***.**.***/admin:3000
This should be working:
**.***.**.***:3000/admin
Note that the requests follow the format:
host:port/directory
Related
it's been more than a day and im stuck on this, please help!
So : I have this hoster that use Phusion Passenger for the NodeJS environnement and i want to run my Strapi App on it.
For now ive did the following :
Strapi and the node modules are installed on the server
The app is connected to the database
the app.js can start fine
The problem is here, i don't know how to configure my app.js file to make it launch Strapi
i did something like this but im pretty sure this is wrong :
const http = require("http");
const hostname = "127.0.0.1";
const port = "passenger";
const strapi = require("#strapi/strapi");
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello World! NodeJS");
});
server.listen(port, hostname, () => {
console.log(`Server running at https://somary.fr/`);
strapi().start();
});
How do i configure my app.js file to finally access my strapi app ?
I wrote a program of node js on Brackets Text Editor and saved it with name first.js. When I am executing it with command prompt using -
node first.js
var http = require('http');
function onRequest(req,res)
{
res.writeHead(200,{'Content-Type':'text/plain'});
res.write('hello js');
res.end();
}
http.createServer(onRequest).listen(8080);
It is running fine but when I am trying to run it on the localhost, it is not working.
The program that I wrote was -
If you did try localhost:8080 and that still doesn't work, it could be that some other program is running on that port. Try .listen(3000) then visit localhost:3000.
you can create a simple http server using express (be sure that u have the packaged installed , you can install express using npm : npm install express) .
your server.js code is :
const express = require("express");
const app = express();
const port = process.env.PORT || 4000;
app.get("/", (req, res) => {
console.log("response");
});
app.listen(port, () => {
console.log("Server listining on port ", port);
});
lets say you save this file in c:/folder , open cmd in the same folder
run : node server.js .
now go to your browser and check : http://localhost:4000/
I want to create a simple Node.js server to do the following :
With my application I just do the command http.get(Node.Js_Server_address/json) to get the json file data stored on my server.
Could please help me with a tutorial? Any help would be appreciated!
This is very simple example of node.js server:
var app = require('./app');
var http = require('http');
var server = http.createServer(app);
server.listen(8080, function() {
console.log("listening to: http://127.0.0.1:8080");
});
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
there is a nice tutorial here and here ...
you can use npm to install node.js and all the packages that you need for it.
hope it helps.
There are lots of examples on this topic, i think you should make some googling before next time.
You can create a REST server via express module of nodeJs. In your server folder use npm install express to download express module. You can get more information about express from here. After that create a server.js file in your server folder.In server.js
var express = require('express');
var app = express();
var PORT = 8080;
/* req stands for request, res stands for response */
app.get('/json',function(req,res){
res.json(yourData);
})
app.listen(PORT,function(){
console.log('Express is listening port:' + PORT + '!');
})
So this should do the work. Let me know if this helps you.
I've done this from scratch and it still gives me an error...
I've run
express test
then
cd test && npm install
I've edited the app.js adding a route such this:
app.get('/test',function(req,res) {
res.writeHead(200, {"Content-Type": "application/json"});
return res.send('{"a":3}');
});
Then I've run node
node app.js
And when I try to access http://server/test I get
Error: Can't set headers after they are sent.
I'm using
Node v4.2.1, Express 2.5.8, npm 3.4.0.
This just happens with Express, if I create a simple server on Node I can use writeHead.
When res.writeHead(200, {"Content-Type": "application/json"});,you send all the response headers to the client,so you can not send header again.
Because res.send is the function of express's response object,I look the source code of this send function:
chunk is what you send here : String('{"a":3}')
this.set('Content-Type', setCharset(type, 'utf-8')) here express helps us concat our content-type with utf-8 so server needs to send header again
That is why you get the error.
Ps.
Sorry for my bad english and I hope you understand what I try to explain.
What about using res.setHeader
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.setHeader("Content-Type","application/json");
res.send('{}');
});
var server = app.listen(3000, function () {
var host = "localhost";
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
I'm using iisnode to host a node app. I'm having trouble actually deploying it under my domain name. Here's the main file with two different starting points. The un-commented code is just a simple server that works correctly when accessed via my domain (so iisnode is mapping and handling the node app correctly). The commented code is the entry point for the express app I am working on, and this works when I view from a local host, but when attempting to access via my domain I receive a 'cannot GET application.js' error.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, world!');
}).listen(process.env.PORT);
//require('./app/init');
//var server = require('./app/server');
//module.exports = server.start(process.env.NODE_ENV);
Here is my server.js file. I think its a routing issue, I've substitued a console.log function for the indexRoute function, and it never fires. But I still don't understand why this works correctly accessing via localhost but not under my domain.
var express = require('express');
var routes = require('./routes');
var app = express();
function createApplication(environment) {
app.get('/', routes.indexRoute);
app.listen(process.env.PORT);
return app;
}
module.exports.start = createApplication;
I can message a git link for full app if anyone is interested.
Try specifying that you want to listen from all IP addresses, not just localhost by adding '0.0.0.0' as a parameter to listen. Also add a callback to see what happened.
app.listen(process.env.PORT, '0.0.0.0', function(err) {
console.log("Started listening on %s", app.url);
});