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/
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 want to check the server.When I tried to run the code in console using:Node server.js, it didn't run the code. Any ideas why?
This is my code where I tried to check the server
const express = require("express");
const app = express();
app.get("/", function(request, response){
console.log(request);
})
app.listen(3000, function () {
console.log("Server started on port 3000");
});
and here I try to run the code in console, and it simply doesn't do anything: it shows me the location and that's all
Helens-MacBook-Pro:my-express-server helenmyrlen$ node server.js
Helens-MacBook-Pro:my-express-server helenmyrlen$
I am also new to NodeJS so I can't find where you are doing are but you can try with the below script. It works.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Run the app with the following command:
$ node app.js
Then, load http://localhost:3000/ in a browser to see the output.
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
My environment: MacOS Mojave
I created a VueJS application from their templates using
vue init pwa frontend
? Project name frontend
? Project short name: fewer than 12 characters to not be truncated on homescreens (default: same as name)
? Project description A Vue.js project
? Author me#myemail.com
? Vue build runtime
? Install vue-router? Yes
? Use ESLint to lint your code? No
? Setup unit tests with Karma + Mocha? No
? Setup e2e tests with Nightwatch? No
yarn
yarn dev
That worked fine. The page ran on localhost:8080 and closed when ctrl-c.
I then built it for production
yarn build
I wrote a quick JS server to launch the contents from the dist/ folder to see what it looks like after building it.
const ora = require('ora')
const chalk = require('chalk');
const express = require('express');
const port = process.env.PORT || 8080;
const app = express();
const spinner = ora(chalk.yellow("Your application is starting..."));
spinner.start();
var delay = ( function() {
var timer = 0;
return function(callback, ms) {
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
app.use(express.static(__dirname + "/dist/"));
app.get(/.*/, function(req, res) {
res.sendFile(__dirname + "/dist/index.html");
});
app.listen(port);
delay(function() {
app.use(express.static(__dirname + "/dist/"));
app.get(/.*/, function(req, res) {
res.sendFile(__dirname + "/dist/index.html");
});
}, 3000);
spinner.stop();
console.log(chalk.cyan("Your application is running here: ") + ("http://localhost:8080"));
I run the server node server.js
However when I ctrl-c it kills the script however the application is still running on localhost:8080
I have tried sudo lsof -i tcp:8080 and netstat -vanp tcp | grep 8080 and they both returned nothing. Which leaves me with the following question: How do I stop this from listening on port 8080?
If anyone was wondering, I found out the problem. I had to go to chrome://serviceworker-internals and chrome://appcache-internals. I did a search for localhost:8080 and killed those serviceworkers.
I hope this helps whoever this happens to next!
I am using Express 4.2.0 and node.js 0.10.12.
The weird thing is that I created a project in C\program files\node\nodetest and when I did npm start I got no errors.
Now I created a project in C\program files\node\secondtest and when I do npm start I get
app.set('port' , process.env.port 3000) typeerror object #<object> has no method 'set' at object.<anonymous> and its pointing in C\program files\node\secondtest\bin\www:5:5
Truth is , I dont know how to deal with this error, because I dont get what it means. Is it because both my projects listen on port 3000?
I just started secondtest , I installed succesfully the dependencies with npm install and added this in app.js
var http = require('http');
var express = require('express');
var app = express();
http.createServer(app).listen(3000, function() {
console.log('Express app started');
});
app.get('/', function(req, res) {
res.send('Welcome!');
});
Thanks
EDIT
If I leave the default code in app.js and www I get no errors. If I replace the default code of app.js with mine, and I remove the
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
part from www, then I get no errors.
Because I guess app.set and app.get are depricated in express 4.2.0? Or because when I set an http server in my app.js code, conflicts the default www code? Either one of these, or I am really confused.
EDIT 2
This is the default code of the www
#!/usr/bin/env node
var debug = require('debug')('secondtest');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
Updated answer according to the updated question.
Since you're calling www and its code needs to set the port and listen to it, your secondtest code should not listen to the port. Instead it should export the Express app as follows:
// ...
module.exports = app;
The www will do the listening part.
Otherwise, the secondtest tries to start listening on a port while not exporting the Express app, and www tries to listen again on a variable app which is not an Express app, thus the error object #<object> has no method 'set'.
When you do var app = require('../app'); in another script, it is important so that this ../app script actually exports the Express app.
Old answer.
Do node app.js instead of using npm command.
Second, make sure the same port is not used by both processes at the same time. You can't listen to the same port unless you're in cluster mode.
Considering the following is the content of both firsttest and secondtest:
var http = require('http');
var express = require('express');
var app = express();
http.createServer(app).listen(process.env.port || 3000, function() {
console.log('Express app started');
});
app.get('/', function(req, res) {
res.send('Welcome!');
});
Do the following to start both apps:
Terminal 1: (the first app will default to port 3000).
$ node firsttest/app.js
Terminal 1:
$ export PORT=3001
$ node secondtest/app.js