简体   繁体   中英

serving a project locally using express

//configuring the file that will serve the file in the source dirrectory
var express = require('express'); //calling express
var  path = require('path'); //reference to path
var open = require('open'); //will be used to open our file in the browser

var port = 3000; //port that will used
var app = express(); //instance of express set to variable app

app.get('/', function(req, res){  //routes express shouls handle: references to the root set to function that takes req and res
  res.sendFile(path.join(__dirname, '../src/index.html'));//calling res.sendFile and use path to join directory name that we are currently running in: joined together with the src dirrectory(../src/index.html)
});

app.listen(port, function(err) { //express to listen to port defined above and adding error handling
  if (err) {
    console.log(err) 
  } else {
    open('http://localhost:' + port);//opening the application on the port
  }
}); //the do: "node buildScript/srcServer.js" to run the file

when i run "node buildScript/srcServer.js". i get the following error:

mygro@COMP4 MINGW64 /e/pluralsight-web-dev/js-dev-environment (master)
$ node buildScript/srcServer.js
internal/modules/cjs/loader.js:969
  throw err;
  ^

Error: Cannot find module 'express'
Require stack:
- E:\pluralsight-web-dev\js-dev-environment\buildScript\srcServer.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
    at Function.Module._load (internal/modules/cjs/loader.js:842:27)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (E:\pluralsi`enter code here`ght-web-dev\js-dev-environment\buildScript\srcServer.js:2:15)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'E:\\pluralsight-web-dev\\js-dev-environment\\buildScript\\srcServer.js'
  ]
}

i have tried even after runing nmp install, nmp install express but it is not working

I think you didn't install express because you used nmp install express use instead npm install express .

I just did npm install express -g

then run node.

thanks for help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM