Is there a way I can render if I add an index.php
We have a couple of experimental projects which do work and allow you to use PHP, but itâs unsupported for now: https://glitch.com/edit/#!/lamp-poc and https://glitch.com/edit/#!/php-poc
Iâm new on glitch. Iâve used php for years. Now that glitch.com doesnât support php I tried learning Nodejs . But W3C thoroughly confused me! When I access the nodejs file which is scripts.js, the server doesnât render it at all. Please help
Please share your project name so we can take a look.
I am not much familiar with node JS and quite honestly, a bit confused about what nodeJS exactly. A Server side scripting language or a server in itself.
Hi @jeet-patil
I would say that Node is a server-side scripting language, but it depends on what you mean with âscripting languageâ. Perhaps it is less ambiguous to say the Node is a server-side runtime that executes code written in JavaScript. Looking at your project, for example, the content of script.js
seem to be written for Node:
var http=require('http');
http.createServer(function server(req,res){
res.write("This is teresa information charlie");
res.end();
}).listen(80);
So this script creates an http server that listens on port 80. Now letâs understand why it is not working for you:
-
Your project does not have a
package.json
. Glitch checks some key files in your repository to understand which kind of project you want to start. If it finds a package.json file, Glitch thinks you are writing a Node application, otherwise it just starts a static web server. The difference is of course that with a static web server you canât have any script running on the server-side. So if you create a new project of type âNode appâ you can copypackage.json
from there and bring it in your project, as a starting point. Glitch will turn your app into a Node app. -
You instructed the http server in script.js to start listening on port 80, which is not available. You should use
process.env.PORT
instead. -
The http server you created always respond the same to any http request you send at him. You probably want something more complex. Again, have a look at the code of the new project of type Node app for more information, or look up a tutorial about express or Node http servers in general. Or ask again here
Having that said, if you want to realize a website and not a web application, that is, if you donât need to have any server-side code running, you are better off with not using package.json
. In this case, all the files in your project will only be executed on the client-side, the browser.
Thanks a lot. I tried the same. But my website immediately stopped working!
logs:
node v6.13.1, with pnpm
InstallingâŚ
Previous installation was done with node v9, recreating node_modules
Packages: +49
+++++++++++++++++++++++++++++++++++++++++++++++++
dependencies:
- express 4.16.3
Total install time: 9794ms
node v9.8.0, with pnpm
InstallingâŚ
Previous installation was done with node v6, recreating node_modules
Packages: +49
+++++++++++++++++++++++++++++++++++++++++++++++++
dependencies:
- express 4.16.3
Total install time: 10900ms
/etc/app-types/node/start.sh: line 33: file.js: command not found
Here is the package.json:
{
â//1â: âdescribes your app and its dependenciesâ,
â//2â: âhttps://docs.npmjs.com/files/package.jsonâ,
â//3â: âupdating this file will download and update your packagesâ,
ânameâ: âmy-glitch-appâ,
âversionâ: â0.0.1â,
âdescriptionâ: âWhat am I about?â,
âscriptsâ: {
âstartâ: âfile.jsâ
},
âdependenciesâ: {
âexpressâ: â^4.16.3â
},
âenginesâ: {
ânodeâ: â9.xâ
},
ârepositoryâ: {
âurlâ: âurl to my appâ
},
âlicenseâ: âMITâ,
âkeywordsâ: [
ânodeâ,
âglitchâ,
âexpressâ
]
}
Youâve to set a valid start script, right now it is file.js
, it should be node file.js
or something like that.
I try once again. Thanks
Hey @jeet-patil!
If @etamponiâs solution doesnât fix the error(s), make sure you named your file correctly. If the file name in your package.json
file isnât the same as your actual file name then it will give an error saying it canât find it.
As for the file @Crazy , Iâve named it correctly
Now I get this error:
node v9.8.0, with pnpm
InstallingâŚ
Total install time: 5835ms
module.js:545
throw err;
^
Error: Cannot find module â/app/file.jsâ
at Function.Module._resolveFilename (module.js:543:15)
at Function.Module._load (module.js:470:25)
at Function.Module.runMain (module.js:690:10)
at startup (bootstrap_node.js:194:16)
at bootstrap_node.js:666:3
module.js:545
throw err;
^
Error: Cannot find module â/app/file.jsâ
at Function.Module._resolveFilename (module.js:543:15)
at Function.Module._load (module.js:470:25)
at Function.Module.runMain (module.js:690:10)
at startup (bootstrap_node.js:194:16)
at bootstrap_node.js:666:3
Iâm sorry but what should this node file.js contain?
This error appears when your main file isnât named the same in your package.json
file. Your package
file should look like this (letâs assume your main fileâs named server.js
):
"main": "server.js",
"scripts": {
"start": "node server.js"
}
Not right away, no. It shouldnât give any errors if empty and the package.json
file is looking for the correct file.
I get it. But no success so far. Iâm tryingâŚ
Mind sending the project link? I can take a look at it.
sure.